[英]Should I create a new Promise in each class method?
I would like to utilize the Promises in my class methods. 我想在我的类方法中使用Promises。 In Promise antipatterns I read that creating a new promise for each new function is considered to be bad.
在Promise反模式中,我读到为每个新函数创建一个新的承诺被认为是坏的。
However, I don't want to return un-related promises in my project, so I thought of doing something like this: 但是,我不想在我的项目中返回不相关的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);
}
)
}
}
Then I would use it like this: 然后我会像这样使用它:
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);
})();
Does this seem like a valid way to do this, or is this an antipattern too? 这看起来像是一种有效的方法吗,或者这也是一个反模式? And if so, how can I avoid writing code like this?
如果是这样,我怎么能避免编写这样的代码?
EDIT: What if I need to make several sequential http calls, perform some actions on the results and then return a Promise based on it? 编辑:如果我需要进行几个连续的http调用,对结果执行一些操作然后返回基于它的Promise怎么办?
The way I understand, if I don't make a new Promise, I have to return the one made by the got.js
library, which includes the http response data. 我理解的方式,如果我不做一个新的 Promise,我必须返回由
got.js
库创建的,包括http响应数据。
Instead, I want to return a Promise which contains the result of my class method. 相反,我想返回一个包含我的类方法结果的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 } ) }
I don't want to return a Promise that contains got.js return values, I want to return my values based on the http request calls. 我不想返回包含got.js返回值的Promise,我想根据http请求调用返回我的值。
async
functions always return a Promise
. async
函数总是返回一个Promise
。
A function/method will return a Promise under the following circumstances: 在以下情况下,函数/方法将返回Promise:
async
. async
。 Since you can await
a Promise and instance.doSomething
is already an async-marked method, you can await it without needing to explicitly return a Promise. 因为您可以
await
Promise和instance.doSomething
已经是异步标记的方法,您可以等待它而无需显式返回Promise。
Simply return
it's result like you would in a regular synchronous method. 只需
return
它的结果,就像在常规同步方法中一样。
I don't want to return un-related promises in my project...
我不想在我的项目中返回不相关的承诺......
Unless you're actually doing something asynchronous in your method (accessing the file system, database calls, timers etc...), you don't need to wrap it in a Promise
, nor await
it when you need a result. 除非您实际上在方法中执行异步操作(访问文件系统,数据库调用,计时器等...), 否则不需要将其包装在
Promise
,也不需要在需要结果时await
它。
The most usual case where you actually need to wrap something in a Promise
is if you have an asynchronous function that works using callbacks but you want to use it as a Promise
. 实际上需要在
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) })()
I've ommited error handling in the above snippet for brevity but you should use throw
in async
-marked methods to raise errors. 为简洁起见,我在上面的代码段中省略了错误处理,但你应该使用
throw
in async
-marked方法来引发错误。 It's the equivalent of calling .reject()
within a Promise. 这相当于在Promise中调用
.reject()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.