A Very Simple Promise Simulator for Old Browsers
Because it works in any javascript standards, and it does'nt matter that your code is executed in old browsers or the new ones.
It can only understand then
, catch
, and finally
.
Here is an example which you can compare native Promise
with this package ((proms
)) in node.js:
"use strict";
const fs = require("fs");
const proms = require("proms");
console.log("======================== Use The JavaScript Built-in Promise");
logDirListPromise()
.then(msg => console.error(`1. ${msg}`))
.then(msg => console.log(`2. ${msg}`))
.catch(msg => console.error(msg))
.then(() => {
console.log("======================== Then Use This Package");
logDirListProms()
.then(msg => console.error(`1. ${msg}`))
.then(msg => console.log(`2. ${msg}`))
.catch(msg => console.error(msg))
.finally(msg /*message of resolve or reject*/ => {
console.log(`The .finally() msg is => ${msg}`);
});
});
function logDirListPromise(){
return new Promise((resolve, reject) => {
fs.readdir("./", (err, files) => {
if(err)
return reject(err);
files.forEach(elem => console.log(elem));
resolve("Hello");
});
});
}
function logDirListProms(){
return new proms((resolve, reject) => {
fs.readdir("./", (err, files) => {
if(err)
return reject(err);
files.forEach(elem => console.log(elem));
resolve("Hello");
});
});
}
Here is a reference for those who want to learn promises, recently.
Contribute with me, guys !
Help me close the current ((or the future)) issues together in github...