Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/theiyd/promnom
π A custom implementation of Javascript Promises
https://github.com/theiyd/promnom
javascript promises
Last synced: 3 days ago
JSON representation
π A custom implementation of Javascript Promises
- Host: GitHub
- URL: https://github.com/theiyd/promnom
- Owner: theIYD
- License: mit
- Created: 2020-07-08T12:12:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T20:55:37.000Z (almost 2 years ago)
- Last Synced: 2024-05-16T01:02:35.987Z (6 months ago)
- Topics: javascript, promises
- Language: JavaScript
- Homepage:
- Size: 150 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## promnom
[![HitCount](http://hits.dwyl.com/theIYD/promnom.svg)](http://hits.dwyl.com/theIYD/promnom)
[![GitHub license](https://img.shields.io/github/license/theIYD/promnom)](https://github.com/theIYD/promnom/blob/master/LICENSE)
[![GitHub stars](https://img.shields.io/github/stars/theIYD/promnom)](https://gitHub.com/theIYD/promnom/stargazers/)A custom implementation of Javascript promises from scratch. The purpose of this implementation was to deeply understand how promises allow asynchronous operations using `setTimeout()` Web API & how a data structure like a queue is maintained.
### Background
A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its `then` method, which registers callbacks to receive either a promiseβs eventual value or the reason why the promise cannot be fulfilled.### Usage
```js
const Promnom = require("./index");
const fs = require("fs");
const path = require("path");const readFile = (filename, enc) =>
new Promnom((resolve, reject) => {
fs.readFile(filename, enc, (err, value) => {
if (err) {
return reject(err);
}
resolve(value);
});
});const delay = (time, value) =>
new Promnom((resolve) => {
setTimeout(() => {
resolve(value);
}, time);
});readFile(path.join(__dirname, "index.js"), "utf-8")
.then((text) => {
console.log(`${text.length} characters read`);
return delay(2000, text.replace(/[aeiou]/g, ""));
})
.then((newText) => {
console.log(newText.slice(0, 200));
})
.catch((err) => {
console.log("An error occurred");
console.error(err);
})
.finally(() => {
console.log("Promise done");
});/*
Output:
3472 characters read
cnst stts = rqr("./stts");// Chck f prms hs thn mthd
cnst sThnbl = (mybPrms) =>
mybPrms && typf mybPrms.thn === "fnctn";// Glbl clss
clss Prmnm {
cnstrctr(cmpt) {
ths._stt = stts.PENDING
Promise done
*/
```### License
MIT