function task1(fullfill, reject) {
console.log('Task1 start');
setTimeout(function() {
console.log('Task1 end');
//fullfill('Task1 result');
reject('Error msg');
}, 300);
}
function fullfilled(result) {
console.log('fullfilled : ', result);
}
function rejected(err) {
console.log('rejected : ', err);
}
new Promise(task1).then(fullfilled, rejected);
我刚开始使用 node.js 并且正在研究 promise 模块(?)。这可能是一个非常基本的问题,但我无法找到满足和拒绝的方法从哪里获取参数的值。
then() 方法返回一个 Promise。它最多需要两个参数:Promise 成功和失败案例的回调函数。
p.then(onFulfilled[, onRejected]);
p.then(function(value) {
// fulfillment
}, function(reason) {
// rejection
});
onFulfilled 如果 Promise 被实现,则调用一个函数。该函数有一个参数,即完成值。onRejected 可选 Promise 被拒绝时调用的函数。这个函数有一个参数,拒绝原因。
let p = function(){
return new Promise(function(resolve, reject) {
if(condition){
// this arg would be caught in onFulfilled
resolve(arg);
}
else{
// this arg would be caught in onRejected
reject(arg2);
}
})
}
为清楚起见,请查看 p
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句