有什么办法可以同步解决诺言吗?(或可以的替代库) [英] Is there any way to resolve a promise synchronously? (or an alternative library that can)

查看:42
本文介绍了有什么办法可以同步解决诺言吗?(或可以的替代库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于验证字符串的方法,我希望该方法返回Promise,因为正在运行的验证可能是异步的.但是,我遇到的问题是性能方面的问题,我希望在可能的情况下(例如:当没有异步验证要完成时)在同一事件循环中解决承诺,但是我希望接口保持一致(例如:始终返回一个承诺).

I have a method for validating a string, I want that method to return a Promise as the validations being ran may be asynchronous. The issue I am having however is one of performance, I want the promise to resolve in the same event loop when possible (eg: when there are no asynchronous validations to be done) but I want the interface to remain consistent (eg: to always return a Promise).

下面的简化代码示例说明了我要尝试执行的操作,但是它会产生上述性能损失,因为即使可以同步执行验证,它仍然会等待下一个事件循环来处理结果.

The simplified code example below illustrates what I'm trying to do, but it incurs the aforementioned performance penalties because even when the validation can be performed synchronously it still waits for the next event loop to process the result.

在我的特定用例中,此性能损失太高了.

In my specific use case this performance penalty is too high.

下面是我正在做的简化(最小)示例

Below is a simplified (minimal) example of what I'm doing

// Array containing validation methods
const validations = [
  (value) => true, // Some validation would happen here
];
// Array containing asynchronous validation methods
const asyncValidations = []; // No async validations (but there could be)
const validate(value){
  // Run synchronous validations
  try {
    validations.forEach(validation => validation(value));
  catch(error){
    // Synchronous validation failed
    return Promise.reject();
  }
  if(asyncValidations){
    return Promise.all(asyncValidations.map(validation => validation(value));
  }
  // Otherwise return a resolved promise (to provide a consistent interface)
  return Promise.resolve(); // Synchronous validation passed 
}

// Example call
validate('test').then(() => {
  // Always asynchronously called
});

推荐答案

您提到两件事:

我希望界面保持一致

I want the interface to remain consistent

  • [我想]总是返回一个诺言

    [I want to] always return a Promise

  • 如果要避免不必要的异步行为,则可以这样做,并使API保持一致.但是您不能做的是总是返回承诺",因为不可能同步地解决承诺".

    If you want to avoid the asynchronous behaviour if it is not needed, you can do that and keep the API consistent. But what you cannot do is to "always return a Promise" as it is not possible to "resolve a promise synchronously".

    您的代码当前返回一个Promise,当不需要进行异步验证时,该Promise将被解决:

    Your code currently returns a Promise that is resolved when there is no need for an async validation:

    // Otherwise return a resolved promise (to provide a consistent interface)
    return Promise.resolve(); // Synchronous validation passed
    

    您可以使用以下代码替换该代码:

    You can replace that code with the following:

    return {then: cb => cb()};
    

    请注意,这只会返回"thenable"(即具有 then 方法)的对象文字,并且会同步执行传递给它的回调.但是,它不会返回承诺.

    Note that this just returns an object literal that is "thenable" (i.e. it has a then method) and will synchronously execute whatever callback you pass it to. However, it does not return a promise.

    您还可以通过实现

    You could also extend this approach by implementing the optional onRejected parameter of the then method and/or the the catch method.

    这篇关于有什么办法可以同步解决诺言吗?(或可以的替代库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆