简体   繁体   English

JavaScript 库中的阻塞/非阻塞函数?

[英]Blocking/non-blocking functions in JavaScript library?

I'm porting a library that includes cpu-expensive functions that need to be able to be used in either synchronous or async mode.我正在移植一个库,其中包含需要能够在同步或异步模式下使用的 CPU 昂贵的函数。 What is the best way to implement these functions in current JavaScript (setTimeout, web-workers, something else) given that they need to work in both node and the browser?考虑到它们需要在节点和浏览器中工作,在当前的 JavaScript(setTimeout、web-workers、其他东西)中实现这些功能的最佳方法是什么?

I would have two versions of each function, and have the async version return a promise rather than accept a callback argument.我将有每个函数的两个版本,并让异步版本返回承诺而不是接受回调参数。

How you do that is up to you.你如何做到这一点取决于你。 You could literally return a promise then resolve/reject it, or you could mark the functions as async which makes them implicitly return a promise, then return the response (which is like resolving with a value).您可以从字面上返回一个承诺,然后解决/拒绝它,或者您可以将函数标记为async ,这使它们隐式地返回一个承诺,然后返回响应(这就像用一个值来解决)。

class LibClass {
  a() { // async a with promises returns Promise<ret>
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const ret = doWork()
        resolve(ret)
      }, 1)
    })
  }
  a_sync() { // sync a returns ret
    const ret = doWork()
    return ret
  }
  async b(data) { // async function b returns Promise<ret>
    const ret = doWork()
    return ret
  }
}

This sounds like you're a bit confused about synchronous and asynchronous.这听起来您对同步和异步有点困惑。 A given function either has a synchronous result or an asynchronous result.给定的函数要么具有同步结果,要么具有异步结果。 One or the other.非此即彼。 That fact is determined by what the function does, not by what a caller thinks or wants.这个事实是由函数的作用决定的,而不是由调用者的想法或想要的决定。

If a function calls anything asynchronous or might call anything asynchronous inside its implementation, then its final result IS asynchronous and there's no way to make it synchronous.如果一个函数调用任何异步或可能在其实现中调用任何异步,则其最终结果是异步的,并且无法使其同步。 It MUST have an asynchronous interface.它必须有一个异步接口。 You can't offer a synchronous interface as the result is simply not available to return synchronously.您不能提供同步接口,因为结果根本无法同步返回。

If a function does not call anything asynchronous inside its implementation, then it's a synchronous function and you should give it a synchronous interface because that's always easier to use than an asynchronous interface and a synchronous interface can also be used with other asynchronous code so it's plenty flexible.如果一个函数在它的实现中没有调用任何异步的东西,那么它就是一个同步函数,你应该给它一个同步接口,因为它总是比异步接口更容易使用,同步接口也可以与其他异步代码一起使用,所以它足够了灵活的。

So, if you had an entirely synchronous library, you could offer both a synchronous and an asynchronous interface, but there is no reason to do so.所以,如果你有一个完全同步的库,你可以同时提供同步和异步接口,但没有理由这样做。 Just offer a synchronous interface as it's always easier to use.只提供一个同步接口,因为它总是更容易使用。

And, ANY function in the library that does anything asynchronous MUST have an asynchronous interface.而且,库中执行任何异步操作的任何函数都必须具有异步接口。

So, the choice is determined by what you're doing in your library functions and nothing else.因此,选择取决于您在库函数中所做的事情,而不是其他任何事情。


FYI, very occasionally, there might be a function that sometimes has a synchronous result and sometimes has an asynchronous result.仅供参考,偶尔,可能有一个函数有时具有同步结果,有时具有异步结果。 The canonical example of this is when a value might already be cached locally and available immediately, but if not in the cache, it has to be retrieved asynchronously from some remote resource.典型的例子是当一个值可能已经在本地缓存并立即可用,但如果不在缓存中,它必须从某个远程资源异步检索。 In this case, you provide an asynchronous interface and, even when the result is immediately available, you still return it asynchronously.在这种情况下,您提供一个异步接口,即使结果立即可用,您仍然异步返回它。 This is so the caller gets one unified interface that works either way and the caller doesn't have to code two separate ways to use your function.这样调用者就可以得到一个统一的接口,可以以任何一种方式工作,并且调用者不必编写两种不同的方式来使用您的函数。 Note: this should be a rare case in a library design, not the usual case.注意:这在库设计中应该是罕见的情况,而不是通常的情况。 Usually, a function is either internally synchronous or asynchronous all the time.通常,一个函数要么一直是内部同步的,要么一直是异步的。

you can use async keyword like :您可以使用 async 关键字,例如:

class LibClass{
      async a(){
         return Promise.resolve('success');
        }
}

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

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