Ö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
})asyncFunc()
.then(result => {
return anotherAsyncFunc(result);
})
.then(chainResult => {
return chainResult.json();
})
.catch(error => {
console.log(error);
})// 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)const makeRequest = async () => {
const value1 = await promise1()
const value2 = await promise2(value1)
return promise3(value1, value2)
}const value;
promise1()
.then(value1 => {
value = value1
return promise2(value1);
})
.then(value2 =>{
return promise3(value, value2);
})By Özgün Bal