gpt4 book ai didi

sequelize.js - 创建行但 promise 不解决

转载 作者:行者123 更新时间:2023-12-03 22:40:59 25 4
gpt4 key购买 nike

使用 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流程图:
enter image description here
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/

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