【发布时间】:2020-01-17 11:40:42
【问题描述】:
这是我实际代码的简化示例,但我试图确保它是有效的。我的问题是关于下面承诺链中的第二项。
// vars photo and image are declared outside of the promise chain
// ...
.then(() => Photo.create()) // this is a promise
.then(p => photo = p) // this is just assigning a variable, no promise
.then(() => image.resize({ height: 240 }).toBuffer()) // this is another promise
// ...
它有效,但这是处理这个问题的好方法吗?我这样组织它的原因是我基本上有一个中间步骤,我需要做一些分配和计算等,只是在组织上我想将它与我的其他 .then() 部分分开,这些部分是实际的承诺。
【问题讨论】:
-
没有。你可以按照你正在做的方式去做,但是当没有异步发生时它没有多大意义。而是调用一个函数,并根据需要将该函数分成单独的函数。
-
是或否,取决于您所说的“承诺链中的一切”是什么意思。
标签: javascript express promise sequelize.js chaining