Open mosaic-greg opened 8 years ago
hey @mosaic-greg.
factory functions are different than callbacks, although they are both ways to use functions.
a factory function is a function that returns a constructed object.
var baseId = 0
function createThing (name) {
return {
id: baseId++
name
}
}
createThing('tree')
// { id: 0, name: 'tree' }
createThing('computer')
// { id: 1, name: 'computer' }
whereas a callback is when you use a function to call something later.
function waitASecond (callback) {
setTimeout(callback, 1000)
}
waitASecond(function () {
console.log('hey!')
})
where the most common pattern for using callbacks is as an "error-first callback", where the first argument to the callback is the error (as in callback(err)
), and any subsequent args are data (as in callback(null, data)
).
so they work really well together. hope this helps!
Hope everyone's doing well. Are factory functions a good alternative to callbacks? https://www.sitepoint.com/factory-functions-javascript/