gpt4 book ai didi

javascript - 链式 bluebird .then() 的奇怪执行顺序

转载 作者:行者123 更新时间:2023-12-03 11:42:57 25 4
gpt4 key购买 nike

有人可以向我解释一下为什么以下两个链式函数:

// returns zero if okay
var resetCounter = function (model) {

return new Promise(function (resolve, reject) {

model.resetCount(function (error, result) {
if (error) {
console.log(error);
reject(error);
} else {
console.log(result);
resolve(result);
}
});
});
};


// returns the amount of deleted entries
var clearCollection = function (collection) {
collection.remove({}, function (error, result) {
if (error) {
console.log(error);
return Promise.reject(error);
} else {
console.log(result);
return Promise.resolve(result);
}
});
};

// chaining for demo purposes
var reset = function () {
return resetCounter(nm)
.then(console.log('1 -> After newsmodel'))
.then(resetCounter(sm))
.then(console.log('2 -> After schoolmodel'))
.then(resetCounter(um))
.then(console.log('3 -> After usermodel'))
.then(clearCollection(nm))
.then(console.log('4 -> After clearing all news'))
.then(clearCollection(sm))
.then(console.log('5 -> After clearing all schools'))
.then(clearCollection(um))
.then(console.log('6 -> After clearing all users'))
.catch(function (error) {
console.log(error);
});
};

产生以下输出:

1 -> After newsmodel
2 -> After schoolmodel
3 -> After usermodel
4 -> After clearing all news
5 -> After clearing all schools
6 -> After clearing all users
7 -> After inserting news
8 -> After inserting schools
9 -> After inserting users
0
0
0
30
500
100

在本例中我省略了一个插入函数。在清除所有内容后,它再次填充数据库...

我的预期结果是:

0
1 -> After newsmodel
0
2 -> After schoolmodel
0
3 -> After usermodel
500
4 -> After clearing all news
100
5 -> After clearing all schools
30
6 -> After clearing all users
7 -> After inserting news
8 -> After inserting schools
9 -> After inserting users

只是为了清理它!在这种情况下,我对 promise 的结果不感兴趣。我只对异步操作的执行顺序感兴趣。这就是为什么我没有将任何 successback 或 errback 传递给 .then()。我尝试用“何时”替换“Bluebird ”,但没有成功,这清楚地告诉我,我错过了关于一般 promise 的重要信息。我遇到的大多数例子都是在下面的 .then() 中使用 Promise 的结果,在这种情况下我又不感兴趣。

我做错了什么?

最佳答案

I am only interested in the execution order of the async operations. That is why I'm not passing any successback or errback to the .then(). Most examples I came around are using the result of the promise in the following .then(), which again, in this case I'm not interested in.

您不必必须使用结果参数,但您始终需要传递一个回调函数,以便 Promise 稍后执行

I'm missing something crucial about promises in general

Promise 确实代表操作的结果,而不是操作本身。这意味着当你做出 promise 时,任务就已经开始了。您需要使用一个可以执行然后返回 promise 的函数

What am I doing wrong?

.then() 方法确实期望这样一个函数 - 当 promise 的结果(无论它是什么)到达时调用。该函数不需要使用该结果,但它必须可调用

就您而言,您正在传递以下内容:

  • console.log(…) - 立即记录参数并传递 undefined
  • resetCounter(um)) - 立即调用异步函数并将 Promise 传递给 then,在此处它被忽略(因为它不是函数)
  • clearCollection(nm) - 立即调用异步函数,但 clearCollection 不返回任何内容,因此我们返回 undefined。<

例如,

// returns the amount of deleted entries
var clearCollection = function (collection) {
collection.remove({}, …);
};

实际上并没有返回任何东西。您使用 return 的唯一部分是回调内部,它没有任何意义。您需要构造一个类似于您的 resetCounter 函数的 Promise - 或者您只需使用 Promisification feature :

function clearCollection(collection) {
return Promise.promisify(collection.remove, collection)({});
}

现在,您的链应该如下所示:

resetCounter(nm)
.then(function() {
console.log('1 -> After newsmodel');
return resetCounter(sm);
})
.then(function() {
console.log('2 -> After schoolmodel');
return resetCounter(um);
})
.then(function() {
console.log('3 -> After usermodel');
return clearCollection(nm);
})
.then(function() {
console.log('4 -> After clearing all news');
return clearCollection(sm);
})
.then(function() {
console.log('5 -> After clearing all schools');
return clearCollection(um);
})
.then(function() {
console.log('6 -> After clearing all users');
}, function (error) {
console.log(error);
});

关于javascript - 链式 bluebird .then() 的奇怪执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26180606/

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