To do this any slow running operation you start will be run on another thread that you have no control over.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs.writeFile('test.txt', 'Hello World', err => { | |
console.log('finished writing file') | |
}) | |
console.log('I happen before it is finished') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (true) { | |
console.log('DOH!') | |
} |
So this is all well and good, and one of the things I like about this approach is it is reasonably simple to understand and get started using node.js. But as time goes on you will probably find some problems.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs.writeFile('test.txt', 'Hello World', err => { | |
database.save('some data', err => { | |
http.call('http://update', err => { | |
}) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function write(file, data, cb) { | |
fs.writeFile(file, data, err => { | |
if (err) return cb(err) | |
database.save(data, err => { | |
if (err) return cb(err) | |
cb() | |
}) | |
}) | |
} | |
write('test.txt', 'Hello World', err => { | |
http.call('http://update', err => { | |
console.log('done!') | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const documents = [ | |
{ name: 'doc1', text: 'hello' }, | |
{ name: 'doc2', text: 'world' }, | |
{ name: 'doc3', text: '!' }, | |
] | |
function write(docs, cb) { | |
const current = docs.pop() | |
fs.writeFile(current.name, current.text, err => { | |
if (err) return cb(err) | |
if (docs.length > 0) { | |
write(docs, cb) | |
} else { | |
cb() | |
} | |
}) | |
} | |
write(docs.slice(), err => { | |
console.log('all done!') | |
}) |
I am by no way saying you shouldn't use callbacks, they are very simple and easy to get started within a new node.js application, but quickly you will probably want to start looking into promises (I'll cover these in a blog soon!). Personally, I usually start off with callbacks and migrate to promises when needed, slight inconsistencies don't worry me too much in this case, but as time goes on I do find the promise interfaces nicer for most things and am mostly using them.
No comments:
Post a Comment