Open phymo opened 2 years ago
const PromiseSimple = require('./promise');
console.log(PromiseSimple);
fakeApiBackend = () => { const user = { userName: 'user', password: 'password', profile: 'ssssssssssssss', };
if (Math.random() > 0.1) { return { data: user, statusCode: 200, }; } else { return { error: 'error', message: 'error, please try later', statusCode: 500, }; } }
const makeApiCall = () => { return new PromiseSimple((resolve, reject) => { setTimeout(() => { const response = fakeApiBackend();
if (response.statusCode === 200) {
resolve(response.data);
} else {
reject(response);
}
}, 3000);
}) }
makeApiCall() .then((data) => { console.log(data); return data; }).then((data) => { console.log(data); }).catch((error) => { console.log(error); });
https://www.lydiahallie.com/blog/promise-execution Long story short, Promises are just objects with some additional functionality to change their internal state.
Promises From The Ground Up https://www.joshwcomeau.com/javascript/promises/
promise.js
module.exports = PromiseSimple