简体   繁体   English

如何从异步函数返回扩展的承诺实例?

[英]How to return an extended promise instance from an async function?

I'm just toying around with some ideas for my framework, lets say I extended the promise class like this我只是在玩弄我的框架的一些想法,假设我像这样扩展了 promise 类

class cool_promise extends Promise {
    cool_function() {
        console.log("I am cool");
    }
}

and had an async function like并有一个异步功能,如

async cool_function() {
    return "functioning";
}

now by default cool_function just returns a regular Promise when executed synchronously but is it possible to make the async functions specific to my framework return my extended promise class instead?现在默认情况下,cool_function 在同步执行时只返回一个常规的 Promise,但是否可以让特定于我的框架的异步函数返回我的扩展 Promise 类?

The async keyword will always cause a function to return a native Promise. async关键字将始终导致函数返回本机 Promise。 To return a custom promise your function needs to be declared without async and return the Promise directly.要返回自定义承诺,您的函数需要在没有async情况下声明并直接返回承诺。

You could write your async functions like normal but wrap them with a function that returns a CoolPromise instead before exposing them as part of your framework's interface:您可以像平常一样编写异步函数,但在将它们作为框架接口的一部分公开之前,将它们用返回 CoolPromise 的函数包装起来:

 class CoolPromise extends Promise { coolFunction ( ) { return 'I am cool'; } static decorate ( func ) { return function (...args) { return CoolPromise.resolve( func.call( this, ...args ) ); }; } } // Some module with an async function in it const module = function Module ( ) { async function foo( ) { return 'bar'; } // Decorate functions before exposing them from your modules return { foo: CoolPromise.decorate( foo ) }; }( ); // Verify that module.foo( ) returns a CoolPromise (async ( ) => { console.log( await module.foo( ) ); console.log( module.foo( ).coolFunction( ) ); })();

No, you would have to convert it manually with cool_promise.resolve(cool_function()) .不,您必须使用cool_promise.resolve(cool_function())手动转换它。 Without using a JavaScript preprocessor, async functions will always return a native Promise .如果不使用 JavaScript 预处理器,异步函数将始终返回本机Promise

An alternative sometimes is to extend the native Promise prototype:有时另一种选择是扩展原生Promise原型:

Object.defineProperty(Promise.prototype, 'cool_function', {
    configurable: true,
    writable: true,
    value() {
        console.log("I am cool");
    },
});

Modifying globals always risks conflicting with other libraries/user code or later changes to the language, though.但是,修改全局变量总是有与其他库/用户代码或以后对语言的更改发生冲突的风险。 Sometimes it's better to just have a standalone function.有时最好有一个独立的功能。 Maybe it'll be possible to use that kind of function in a pipeline similar to the method call syntax someday – for example, there's a proposal that would let you do this or something like it:也许有一天可以在类似于方法调用语法的管道中使用这种函数 - 例如,有一个建议可以让你这样做或类似的事情:

const cool_extension = (promise) => {
    console.log("I am cool");
};

cool_function() |> cool_extension
// cool_function().cool_extension(), when extending prototype

And in the meantime, cool_extension(cool_function()) is not too bad and comfortably familiar.同时, cool_extension(cool_function())还不错,而且很熟悉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM