gpt4 book ai didi

javascript - 跳出 promise 链或走不同的道路

转载 作者:行者123 更新时间:2023-11-30 15:23:16 27 4
gpt4 key购买 nike

我有一个问题,我可能需要因为某些值(value)而跳出整个 promise 链,或者根据一个值(value)采取两条路径。

你怎么做最好?

这是我想跳出整个链条的第一个场景。我只想给他们留言。

      DB_WorkIssues.info().then(function (details) {
if (details.doc_count == 0 && details.update_seq == 0) {
showMsg("This device has no local data on it and no access to the Server, please come back when you are online.")
} jump out here, no need to do the next set.
else
return; Continue on as the values are valid.
}).then(function () {
return ajaxCallForJson(URI_LookupTables);
}).then(function (json) {
return callBulkDocLoad(DB_LookupTables, json);
}).then(function () {
return loadCategoriesDDL();
}).then(function () {
return loadEquipmentDDL();
}).catch(function (err) {
showMsg("Error in defineDBs: " + err);
});

在第二种情况下,如果值是一回事,我可能想走一条路,如果值是另一回事,我可能想走另一条路。但我仍然希望链条能够与第一个 promise 一起工作。像这样:

      DB_WorkIssues.info().then(function (details) {
if (details.doc_count == 0 && details.update_seq == 0) {

Take this path.
return;
}).then(function () {
return ajaxCallForJson(URI_LookupTables);
}).then(function (json) {
return callBulkDocLoad(DB_LookupTables, json);
}).catch(function (err) {
showMsg("Error in defineDBs: " + err);
});

}
else
{

Take this path instead

return;
}).then(function () {
return loadCategoriesDDL();
}).then(function () {
return loadEquipmentDDL();
}).catch(function (err) {
showMsg("Error in defineDBs: " + err);
});
}

谢谢。

这是我在查看答案后的想法,我总是做第二个 promise ,在某些情况下只做第一个。

    DB_WorkIssues.info().then(function(details) {

// promise variable , defined in conditional
var promise;

Would I set the promise to some default value, in case the following test fails

if (details.doc_count == 0 && details.update_seq == 0) {
// return this promise
promise = ajaxCallForJson(URI_LookupTables).then(function(json) {
return callBulkDocLoad(DB_LookupTables, json);
});

}
return promise;

}).then(function () {
return loadCategoriesDDL();
}).then(function () {
return loadEquipmentDDL();
}).then(function () {
return loadLocationsDDL();
}).catch(function (err) {
showMsg("Error in defineDBs: " + err);
});

我能做到吗?

谢谢。

最佳答案

我认为这是一个骨架,代表了您的目标。 Promise 非常强大,值得研究。我尝试添加有用的评论,但我建议尝试使用代码并了解正在发生的事情。

// Six named promise-creators. When called with (x), will create a promise
// which waits 200ms and then logs and resolves with (x).
// These could represent any asynchronous operation.
const p1 = p2 = p3 = p4 = p5 = p6 =
(x) => {
const p = new Promise((resolve, reject) => {
setTimeout(() => {resolve(x); console.log(x)}, 200)
});
return p;
}

// A function which, when called, will execute first promise chain.
const first_steps = () =>
p1(1)
.then(result => p2(2))
.then(result => p3(3))

// A function which, when called, will execute second promise chain.
const second_steps = () =>
p4(4)
.then(result => p5(5))
.then(result => p6(6))

// When true, this prints numbers 1-6.
// When false, only prints numbers 4-6.
if (false) {
console.log(first_steps().then(second_steps));
} else {
second_steps();
}

关于javascript - 跳出 promise 链或走不同的道路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43381258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com