Özgün Bal
Software Developer, Javascript Enthusiast
Solves these
// Company A's API that you're using in your application
function untrustedAPIMethod (paramX, paramY, callback) {
// 1 - If it is not actually asynchronous, may called too early
callback();
// 2- Callback is not called at all due to buggy implementation
// 3- Can be called more than needed
for(let i=0; i< task.length; i++) {
callback();
}
// 4- Fail to pass parameters
callback();
// or
callback(null); // instead of error first callback: callback(null, result)
// 5- Swallow errors
const result = `I\'m not an arry type but .map() method
is belong to Array.prototype`.map(letter => console.log(letter))
// Before callback, TypeError is throwed and and callback never called
callback(result);
}const promise = new Promise(function(resolve, reject){
if (satisfiedCondition) {
resolve('Task is done');
} else {
reject('Error occured');
}
})
promise.then(function(data){
console.log(data) // prints 'Task is done' when promise is resolved
}).catch(function(error){
console.log(error) // prints 'Error occured' when promise is rejected
})// Immediately resolved promise object
const resolved = Promise.resolve(5);
// Immediately rejected promise object
const rejected = Promise.reject('Oops');
// Takes array of promises and resolves if all of them resolved otherwise rejects
const allFulfilled = Promise.all([resolved, rejected]);
// Returns first resolved/rejected promise in array of promises
const raceOfPromises = Promise.race(promiseArray)By Özgün Bal