- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以向我解释一下为什么以下两个链式函数:
// 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/
一晃五年没写博客了,依旧再C#上耕耘,依旧没有啥建树,现在也不知道.net上还有多少人再使用,在这里分享一些自己觉得写的还算优雅的代码。 对于自己写着完的代码,我特别喜欢链式(来源于jQuer
我正在构建一个吉他和弦查找应用程序。我使用多维数组来表示指板。数组中的每个元素都由具有字符串属性“Note”的 FretSpace 结构表示。为了初始化指板上的音符属性,我传递了要处理的吉他弦的详细信
我在演示代码中使用 setTimeout 函数模拟了 3 个 ajax 调用。我将从一段运行良好的代码开始:所有调用都是并行进行的,我希望所有调用都能成功,否则会出现错误。 var p1 = func
谁能解释一下? a = [2,3,4] b = [5,6,8,9] print(len(a) > 0) print(len(b) > 0) print((len(a) > 0) & len(b) >
我正在处理具有多个子 JSONObject 的 JSONObject。这是我填写内容的方式: myJson.getJSONObject(CAT_NAME).put(VAR_NAME, var)
想象一下这种情况,我有一个需要检查属性的对象。但是,该对象当前可以具有空值。 如何在一个“if”条件下检查这两个条件? 目前,我必须做这样的事情: if (myObject != null) {
我有一个对象集合,称它们为obj。他们有一个 act() 方法。 act() 方法最终会导致 o 上的 event() observable 调用 onComplete。 链接这些的好方法是什么? 即
假设我有一个列表变量 datalist 存储 10,000 个字符串实体。QTableView 只需要显示其中的一些实体。这就是为什么 QTableView 被指定为 QSortFilterProxy
我正在寻找支持链式 MSI 安装的工具(最好不是 InstallShield,而且最好是便宜/免费的)。我有几个小型安装需要能够单独部署,但也需要作为一个组部署,我不想维护多个安装程序。 看起来我需要
在这种情况下,我想迭代集合中除最后 2 个元素之外的所有元素。 假设我采用了一种奇怪的方式,例如 x.Reverse().Skip(2).Reverse()。 每个 LINQ 操作是否会有效地生成一个
对于javascript来说非常陌生,我有两个html数字选择,包括年份,我想将第二个选择与第一个选择链接起来,这样当我在第一个选择中选择年份时(而第二个选择没有选项)首先),第二个选择应包括从所选数
有人可以向我解释一下为什么以下两个链式函数: // returns zero if okay var resetCounter = function (model) { return new Prom
所以我有 2 个 promise 函数。当第一个函数出现错误时,我希望它显示错误消息。当完成或失败时,我希望他们执行一个finally catch all 函数,但由于某种原因它不起作用。我的代码如下
我有一个函数 const func = () => server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyN
(async function() { var a,b; function flush(){ return new Promise(res => {
这个问题已经有答案了: Promise chaining: Use result from previous promise in next then callback [duplicate] (1
这可能不是专业正则表达式理解的问题。唯一重要的是因为我正在运行多个链式替换命令,这些命令会影响文本文件中的某些相同文本。我还想象在替换之前,根据分隔符词(需要多次替换)的使用方式对 txt 文件进行分
我正在尝试构建一组类来定义 OSI 堆栈中协议(protocol)的分层属性...从抽象意义上讲,我只需要从父 python 类继承属性,但我需要能够调用整个类链一次...所以,我正在寻找这样的东西.
我正在努力兑现 promise ,到目前为止我偶然发现了这一点: new Promise((resolve, reject) => { setTimeout(() => { r
我试图理解 promise ,我需要链接它们并装饰来自不同端点的对象宽度数据。 例如: 我的 Node-express 应用程序中有这个 //controller.js export const ge
我是一名优秀的程序员,十分优秀!