function fetchStuff(callback) {
setTimeout(() => {
callback('Look what I found!');
}, 500);
}
fetchStuff(fetchedData => console.log(fetchedData));
function fetchStuff(callback) {
setTimeout(() => {
throw 'I found nothing :(';
}, 500);
}
try {
fetchStuff(fetchedData => console.log(fetchedData));
} catch (e) {
console.log(e);
}
function fetchStuff(callback) {
setTimeout(() => {
try {
// some black magic
callback(undefined, 'I found data');
} catch (error) {
callback(error);
}
}, 500);
}
fetchStuff((error, fetchedData) => {
error ? console.log('Outch') : console.log(fetchedData);
});
asyncFetch((error, fetchedData) => {
// Do something with fetched Data
asyncPost(data, (error2, result) => {
// Do something with result
asyncFetch((error3, fetchedData3) => {
console.log(error); // Wat?
});
});
});
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Found data'), 500);
});
myPromise
.then(data => console.log(data));
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => throw 'No data found :(');
});
myPromise
.then(
success => /* Do something */,
error => console.error(error)
);
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Found something');
});
myPromise
.then(
result => throw 'Ooops!',
error => console.error(error)
);
myPromise
.then(() => {
// Do something
return asycPush();
})
.catch(error => console.error(error));
.then((resultOfOtherPromise) => {
return asyncFetch();
})
Promise.all([
myFirstPromise,
mySecondPromise,
])
.then((promiseResults) => {
/* Array of Promise results */
})
.catch(error => console.error(error);
const value1 = undefined;
const value2 = null;
if(value1 == value2) {
console.log('Y though?');
}
const customer = getCustomer();
if(customer === null || customer === undefined) {
// do sth.
}
if(!customer) {
// do sth.
}
If you want to know the specifics of type coercion check out You don't know JS