如何在 node.js 中创建自定义异步函数? [英] How do you create custom asynchronous functions in node.js?
问题描述
我不确定 node.js 如何能够实现哪些功能是异步的,哪些不是,以及如何创建自定义异步功能.
I was unsure how node.js was able to realize what functions where async and which were not and how to create a custom async function.
假设我想创建一个自定义异步函数.如果仅仅因为我调用了异步函数回调或 cb 的最后一个参数,它就会知道它是一个异步函数,我会感到惊讶:
Say I wanted to create a custom asynchronous function. I would be surprised if just because I called my last argument to the async function callback or cb that it would just know its an async function:
function f(arg1, callback){
//do stuff with arg1
arg1.doStuff()
//call callback
callback(null, arg1.result());
}
我尝试过类似的东西,但它没有异步工作.你如何告诉 node.js f 实际上是异步的?
I tried something like that and it did not work async. How do you tell node.js that f is actually async?
推荐答案
NOTE: 这个答案写于 2014 年,在 async function
存在之前,在 Promises 之前获得人气.虽然同样的原则也适用,但我建议您先阅读 Promises,然后再尝试了解它们与传统"回调驱动的异步函数的关系.
NOTE: this answer was written in 2014, before the existence of async function
, and before Promises gaining popularity. While the same principles apply as well, I would recommend reading on Promises before trying to get your head around how they relate to "traditional" callback-driven async functions.
要创建一个异步调用其回调的函数,您必须在其上使用一些平台提供的异步原语(通常与 IO 相关) - 计时器、从文件系统读取、发出请求等.
To create a function that calls its callback asynchronously, you have to use some platform-provided async primitive (typically IO-related) on it - timers, reading from the filesystem, making a request etc.
例如,这个函数接受一个回调参数,并在 100 毫秒后调用它:
For example, this function takes a callback argument, and calls it 100ms after:
function asyncFn(callback) {
setTimeout(() => {
callback();
}, 100);
}
在不需要时使函数异步的一个可能原因是为了 API 一致性.例如,假设您有一个函数发出网络请求,并缓存结果以供以后调用:
A possible reason for making a function async when it doesn't need to be, is for API consistency. For example, suppose you have a function that makes a network request, and caches the result for later calls:
var cache = null;
function makeRequest(callback) {
if (!cache) {
makeAjax(result => {
cache = result;
callback(result);
});
} else {
callback(cache);
}
}
问题是,这个函数不一致:有时是异步的,有时不是.假设您有这样的消费者:
The problem is, this function is inconsistent: sometimes it is asynchronous, sometimes it isn't. Suppose you have a consumer like this:
makeRequest(result => doSomethingWithResult(result));
doSomethingElse();
doSomethingElse
函数可以在 doSomethingWithResult
函数之前或之后运行,具体取决于结果是否被缓存.现在,如果您在 makeRequest
函数上使用异步原语,例如 process.nextTick
:
The doSomethingElse
function may run before or after the doSomethingWithResult
function, depending on whether the result was cached or not. Now, if you use an async primitive on the makeRequest
function, such as process.nextTick
:
var cache = null;
function makeRequest(callback) {
if(!cache) {
makeAjax(result => {
cache = result;
callback(result);
});
} else {
process.nextTick(() => callback(cache));
}
}
调用总是异步的,doSomethingElse
总是在 doSomethingWithResult
之前运行.
The call is always async, and doSomethingElse
always runs before doSomethingWithResult
.
这篇关于如何在 node.js 中创建自定义异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!