Created
September 6, 2018 00:14
-
-
Save kmoskwiak/aa336ab7318053d3156477b7d09ea9a4 to your computer and use it in GitHub Desktop.
Asynchroniczność
This file contains 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 FORCE_ERROR = false; | |
function countUsersInDatabase() { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
if(FORCE_ERROR) { | |
return reject(new Error('database error')); | |
} else { | |
return resolve(44); | |
} | |
}, 3000); | |
}); | |
} | |
async function runCode() { | |
var numberOfUsers; | |
try { | |
result = await countUsersInDatabase(); | |
numberOfUsers = result; | |
console.log(numberOfUsers); | |
} catch(err) { | |
console.error(err); | |
} | |
} | |
var sumOfTwoNumbers; | |
runCode(); | |
sumOfTwoNumbers = 10 + 5; | |
console.log(sumOfTwoNumbers); |
This file contains 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 FORCE_ERROR = false; | |
function countUsersInDatabase(callback) { | |
setTimeout(function() { | |
if(FORCE_ERROR) { | |
return callback(new Error('database error')); | |
} else { | |
return callback(null, 44); | |
} | |
}, 3000); | |
} | |
var numberOfUsers; | |
var sumOfTwoNumbers; | |
countUsersInDatabase(function(err, response) { | |
if(err) { | |
return console.error(err); | |
} | |
numberOfUsers = response; | |
console.log(numberOfUsers); | |
}); | |
sumOfTwoNumbers = 10 + 5; | |
console.log(sumOfTwoNumbers); |
This file contains 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 FORCE_ERROR = false; | |
function countUsersInDatabase() { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
if(FORCE_ERROR) { | |
return reject(new Error('database error')); | |
} else { | |
return resolve(44); | |
} | |
}, 3000); | |
}); | |
} | |
var numberOfUsers; | |
var sumOfTwoNumbers; | |
countUsersInDatabase() | |
.then(function(response) { | |
numberOfUsers = response; | |
console.log(numberOfUsers); | |
}) | |
.catch(function(err){ | |
console.error(err); | |
}); | |
sumOfTwoNumbers = 10 + 5; | |
console.log(sumOfTwoNumbers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment