[英]Should synchronous code called by Promise .then create a new Promise
[英]Should I create a new Promise in each class method?
我想在我的类方法中使用Promises。 在Promise反模式中,我读到为每个新函数创建一个新的承诺被认为是坏的。
但是,我不想在我的项目中返回不相关的promise,所以我想做这样的事情:
class MyClass {
async getToken() {
return new Promise(
(resolve, reject) => {
// . . .
const token_str = '<response_from_http_request>';
resolve(token_str);
}
)
}
async doSomething(token) {
return new Promise(
(resolve, reject) => {
const result = // . . .
resolve(result);
}
)
}
async doAnotherSomething(token) {
return new Promise(
(resolve, reject) => {
const result = // . . .
resolve(result);
}
)
}
}
然后我会像这样使用它:
let instance = new MyClass();
(async () => {
const token = await instance.getToken();
const result1 = await instance.doSomething(token);
console.log(result1);
const result2 = await instance.doAnotherSomething(token);
console.log(result2);
})();
这看起来像是一种有效的方法吗,或者这也是一个反模式? 如果是这样,我怎么能避免编写这样的代码?
编辑:如果我需要进行几个连续的http调用,对结果执行一些操作然后返回基于它的Promise怎么办?
我理解的方式,如果我不做一个新的 Promise,我必须返回由got.js
库创建的,包括http响应数据。
相反,我想返回一个包含我的类方法结果的Promise。
例: async getCityWeather( city_name ) { return new Promise( (resolve, reject) => { // get the city id based on its name const city_id = await got(`https://my-api/getCityIdByName/${city_name}`); // now get the weather info for the city with id `cityid` const weather_info = await got(`https://my-api/weatherById/${city_id}`); // make an object to return const temperature = { weather_info.temp_min, weather_info.temp_max, } resolve(temperature); // ... all error handling are omitted } ) }
我不想返回包含got.js返回值的Promise,我想根据http请求调用返回我的值。
async
函数总是返回一个Promise
。
在以下情况下,函数/方法将返回Promise:
async
。 因为您可以await
Promise和instance.doSomething
已经是异步标记的方法,您可以等待它而无需显式返回Promise。
只需return
它的结果,就像在常规同步方法中一样。
我不想在我的项目中返回不相关的承诺......
除非您实际上在方法中执行异步操作(访问文件系统,数据库调用,计时器等...), 否则不需要将其包装在Promise
,也不需要在需要结果时await
它。
实际上需要在Promise
包装内容的最常见情况是,如果你有一个使用回调函数的异步函数,但你想将它用作Promise
。
// plain old callback-style asynchronous functions: const getFooViaCallback = callback => { setTimeout(() => { callback('foo') }, 150) } const getBarViaCallback = callback => { setTimeout(() => { callback('bar') }, 150) } class Foo { constructor() {} getFooViaPromise() { // wrap callback-style code in a Promise // so we can await it. return new Promise(resolve => { getFooViaCallback(result => { resolve(result) }) }) } getBarViaPromise() { // wrap callback-style code in a Promise // so we can await it. return new Promise(resolve => { getBarViaCallback(result => { resolve(result) }) }) } getBaz() { // no reason to wrap this in a Promise, // since it's a synchronous method. return 'baz' } async getFooBarBaz() { const foo = await this.getFooViaPromise() const bar = await this.getBarViaPromise() const baz = this.getBaz() return foo + ',' + bar + ',' + baz } } ;(async() => { const foo = new Foo() const result = await foo.getFooBarBaz() console.log('foo.getFooBarBaz() result: ', result) })()
为简洁起见,我在上面的代码段中省略了错误处理,但你应该使用throw
in async
-marked方法来引发错误。 这相当于在Promise中调用.reject()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.