使用 seqalizerjs,解决一个 promise :
return new Promise(function(resolve, reject){
return models.merchants.create({
name: ctx.name,
});
}).then(function(result){
resolve({
id: result.id
});
}).catch(function(err){
reject(err);
});
以 chai 作为 promise 进行测试
return user.save(data).should.eventually.equal('123123');
但我总是得到这个:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure
it resolves.
最佳答案
我认为问题出在这一行:
return new Promise(function(resolve, reject){
return models.merchants.create({
name: ctx.name,
});
// .....
Promise
函数应该有 resolve()
,像这样:return new Promise(function(resolve, reject){
resolve(models.merchants.create({
name: ctx.name,
}));
基于 this reference :
The resolve and reject functions are bound to the promise and calling them fulfills or rejects the promise, respectively.
仍然基于引用,有
Promise
流程图:then()
或 catch()
之后 Promise()
不处理 resolve()
也不是 reject()
,它们只处理从 resolve()
传递的值或 reject()
.所以,我认为你的代码应该是这样的:
return new Promise(function(resolve, reject){
resolve(models.merchants.create({
name: ctx.name,
}));
}).then(function(merchants){
console.log(merchants) // the created `merchants` from the `resolve()` of the promise would be passed here.
return merchants;
});
更新
您的代码应如下所示:
return new Promise(function(resolve, reject){
models.merchants.create({ // assuming `models.merchants.create()` is an async method
name: ctx.name,
}).then(function(result){
resolve({
id: result.id
});
}).catch(function(err){
reject(err);
});
关于sequelize.js - 创建行但 promise 不解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46597811/