Synchronous vs Asynchronous programming in nodejs All Versions
v0.1
v0.2
v0.3
v0.4
v0.5
v0.6
v0.7
v0.8
v0.9
v0.10
v0.11
io.js v1.0
io.js v1.1
io.js v1.2
io.js v1.3
io.js v1.4
io.js v1.5
io.js v1.6
io.js v1.7
io.js v1.8
io.js v2.0
io.js v2.1
io.js v2.2
io.js v2.3
io.js v2.4
io.js v2.5
io.js v3.0
io.js v3.1
io.js v3.2
io.js v3.3
v4.0
v4.1
v4.2
v5.0
v5.1
v5.2
v5.3
v5.4
v5.5
v0.12
v4.3
v5.6
v5.7
v4.4
v5.8
v5.9
v5.10
v5.10.1
v5.11.0
v6.0.0
v5.11.1
v6.1.0
v6.2.0
v6.2.1
v6.2.2
v5.12.0
v6.3.0
v6.3.1
v6.4.0
v6.5.0
v6.6.0
v6.7.0
v6.8.0
v6.8.1
v6.9.0
v6.9.1
v7.0.0
v7.1.0
v7.2.0
v6.9.2
v7.3.0
This draft deletes the entire topic.
expand all
collapse all
Examples
-
The async package provides functions for asynchronous code.
Using the auto function you can define asynchronous relations between two or more functions:
var async = require('async'); async.auto({ get_data: function(callback) { console.log('in get_data'); // async code to get some data callback(null, 'data', 'converted to array'); }, make_folder: function(callback) { console.log('in make_folder'); // async code to create a directory to store a file in // this is run at the same time as getting the data callback(null, 'folder'); }, write_file: ['get_data', 'make_folder', function(results, callback) { console.log('in write_file', JSON.stringify(results)); // once there is some data and the directory exists, // write the data to a file in the directory callback(null, 'filename'); }], email_link: ['write_file', function(results, callback) { console.log('in email_link', JSON.stringify(results)); // once the file is written let's email a link to it... // results.write_file contains the filename returned by write_file. callback(null, {'file':results.write_file, 'email':'[email protected]'}); }] }, function(err, results) { console.log('err = ', err); console.log('results = ', results); });
This code could have been made synchronously, by just calling the
get_data
,make_folder
,write_file
andemail_link
in the correct order. Async keeps track of the results for you, and if an error occurred (first parameter ofcallback
unequal tonull
) it stops the execution of the other functions.
Please consider making a request to improve this example.
Still have a question about Synchronous vs Asynchronous programming in nodejs?
Ask Question
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draftlang-js