没有 return 数据的函数的承诺
Promises for a function with no return data
我正在尝试为节点理清一大堆基于回调的代码,由于我有很多异步数据库操作,所以 promises 似乎是其中的关键。具体来说,我正在使用 Bluebird。
我被困在如何处理需要从数据库检索数据并在 this
上设置特定值的函数上。我想要实现的最终目标是这样的:
myobj.init().then(function() {
return myobj.doStuff1();
}).then(function() {
return myobj.doStuff2();
}).catch(function(err) {
console.log("Bad things happened!", err);
});
特别是init
、doStuff1
和doStuff2
需要运行只在前一个完成时才需要,但它们都是(多个)异步操作。
到目前为止,这是我对 init 的了解,但我不知道如何完成它:
Thing.prototype.init = function(force) {
if (!this.isInitialized || force) {
return datbase.query("...").then(function(results){
// ... use results to configure this
}).catch(function(err){
console.log("Err 01");
throw err;
});
} else {
// ???
// No data needs to be retrieved from the DB and no data needs to be returned per-se because it's all stored in properties of this.
// But how do I return something that is compatible with the other return path?
}
}
编辑: 虽然链接的重复问题解释了类似的模式,但它并没有完全回答我的问题,因为它没有说清楚我可以什么都不做就解决一个承诺.
如果我正确理解你的问题,你可以这样做:
Thing.prototype.init = function(force) {
if (!this.isInitialized || force) {
return datbase.query("...").then(function(results){
// ... use results to configure this
}).catch(function(err){
console.log("Err 01");
reject(err);
throw err;
});
} else {
// ???
// No data needs to be retrieved from the DB and no data needs to be returned per-se because it's all stored in properties of this.
// But how do I return something that is compatible with the other return path?
return Promise.resolve();
}
}
}
只需 return Promise.resolve();
来自您的 else 函数。
我正在尝试为节点理清一大堆基于回调的代码,由于我有很多异步数据库操作,所以 promises 似乎是其中的关键。具体来说,我正在使用 Bluebird。
我被困在如何处理需要从数据库检索数据并在 this
上设置特定值的函数上。我想要实现的最终目标是这样的:
myobj.init().then(function() {
return myobj.doStuff1();
}).then(function() {
return myobj.doStuff2();
}).catch(function(err) {
console.log("Bad things happened!", err);
});
特别是init
、doStuff1
和doStuff2
需要运行只在前一个完成时才需要,但它们都是(多个)异步操作。
到目前为止,这是我对 init 的了解,但我不知道如何完成它:
Thing.prototype.init = function(force) {
if (!this.isInitialized || force) {
return datbase.query("...").then(function(results){
// ... use results to configure this
}).catch(function(err){
console.log("Err 01");
throw err;
});
} else {
// ???
// No data needs to be retrieved from the DB and no data needs to be returned per-se because it's all stored in properties of this.
// But how do I return something that is compatible with the other return path?
}
}
编辑: 虽然链接的重复问题解释了类似的模式,但它并没有完全回答我的问题,因为它没有说清楚我可以什么都不做就解决一个承诺.
如果我正确理解你的问题,你可以这样做:
Thing.prototype.init = function(force) {
if (!this.isInitialized || force) {
return datbase.query("...").then(function(results){
// ... use results to configure this
}).catch(function(err){
console.log("Err 01");
reject(err);
throw err;
});
} else {
// ???
// No data needs to be retrieved from the DB and no data needs to be returned per-se because it's all stored in properties of this.
// But how do I return something that is compatible with the other return path?
return Promise.resolve();
}
}
}
只需 return Promise.resolve();
来自您的 else 函数。