我想创建以下的Promise语法:
我希望在post函数之后调用第二个then
,我该怎么做?
randomBytes.then(function() {
return requestPromise.post(....);
}).then(function() {
// I want this part to be called after the POST
});
我想创建以下的Promise语法:
我希望在post函数之后调用第二个then
,我该怎么做?
randomBytes.then(function() {
return requestPromise.post(....);
}).then(function() {
// I want this part to be called after the POST
});
.post
返回一个承诺),这就是then
的重点 - 它允许您等待事情发生。then
返回一个承诺,链式结构将会假定其状态,因此只有在它完成后才会调用以下部分。randomBytes.then(function() {
return requestPromise.post(....);
}).then(function() {
// WILL ALWAYS BE CALLED AFTER THE POST
});
它应该可以工作。 你可以添加一个“then”来获取提交的值;
randomBytes.then(function() {
return requestPromise.post(....).then((value,...)=>{return value;});
}).then(function(value) {
//console.log(value);
});
.then((value,...
部分是多余的。(如果你要编写这样的函数,可以更紧凑地编写为value => value
; 这样写更明显它并没有完成任何事情。) - user663031
requestPromise.post
返回一个 Promise,那么你的代码应该按照你想要的方式工作。 - dfsqthen
时指定的处理程序。因此,正确的描述是“返回承诺的承诺处理程序”,正如其他人所说,这在承诺规范中被明确定义并且完全可以接受。所以你写的没问题。你试过了吗?你有什么问题? - user663031