Skip to content

Commit 1bd15ea

Browse files
New Exercise: Promises (#932)
Co-authored-by: Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info>
1 parent 1990253 commit 1bd15ea

File tree

11 files changed

+360
-0
lines changed

11 files changed

+360
-0
lines changed

config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,16 @@
906906
"strings"
907907
]
908908
},
909+
{
910+
"slug": "promises",
911+
"name": "Promises",
912+
"uuid": "ad21018c-e538-4ce5-a33a-949b4293df8c",
913+
"core": false,
914+
"practices": [],
915+
"prerequisites": [],
916+
"difficulty": 4,
917+
"topics": ["promises", "error_handling", "higher_order_functions"]
918+
},
909919
{
910920
"slug": "yacht",
911921
"name": "Yacht",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
Given the basic `Promise`, implement the following functions:
4+
5+
- `promisify`: takes a callback that is turned into a function that returns a `Promise`
6+
- `all`: takes an array of promises and resolves when _all_ of them are resolved, or rejects when _one_ of them rejects.
7+
- `allSettled`: takes an array of promises and resolves when _all_ of them either resolve or reject.
8+
- `race`: takes an array of promises and resolves or rejects with the value of the _first_ promise that resolves or rejects.
9+
- `any`: takes an array of promises and resolves when _one_ of them resolves, or rejects when _all_ of them reject.
10+
11+
Do not use built-in `Promise` functions other than creating a `Promise`, `then` and `catch`.

exercises/practice/promises/.eslintrc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"globals": {
9+
"BigInt": true
10+
},
11+
"env": {
12+
"es6": true,
13+
"node": true,
14+
"jest": true
15+
},
16+
"extends": [
17+
"eslint:recommended",
18+
"plugin:import/errors",
19+
"plugin:import/warnings"
20+
],
21+
"rules": {
22+
"linebreak-style": "off",
23+
24+
"import/extensions": "off",
25+
"import/no-default-export": "off",
26+
"import/no-unresolved": "off",
27+
"import/prefer-default-export": "off"
28+
}
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"blurb": "Compute the prime factors of a given natural number.",
3+
"authors": ["slaymance"],
4+
"contributors": ["SleeplessByte"],
5+
"files": {
6+
"solution": ["promises.js"],
7+
"test": ["promises.spec.js"],
8+
"example": [".meta/proof.ci.js"]
9+
}
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export const promisify = (fn) => (...args) =>
2+
new Promise((resolve, reject) =>
3+
fn(...args, (err, result) => (err ? reject(err) : resolve(result)))
4+
);
5+
6+
export const all = (promises) =>
7+
promises.reduce(
8+
async (acc, promise) => (await acc).concat(await promise),
9+
Promise.resolve([])
10+
);
11+
12+
export const allSettled = (promises) =>
13+
promises.reduce(
14+
async (acc, promise) =>
15+
(await acc).concat(await promise.catch((err) => err)),
16+
Promise.resolve([])
17+
);
18+
19+
export const race = (promises) =>
20+
new Promise((resolve, reject) =>
21+
promises.forEach((promise) => promise.then(resolve, reject))
22+
);
23+
24+
export const any = (promises) =>
25+
new Promise((resolve, reject) => {
26+
promises.forEach((promise) => promise.then(resolve).catch(() => null));
27+
allSettled(promises).then(reject);
28+
});

exercises/practice/promises/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false

exercises/practice/promises/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
useBuiltIns: 'entry',
10+
corejs: 3,
11+
},
12+
],
13+
],
14+
plugins: ['@babel/plugin-syntax-bigint'],
15+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@exercism/javascript-promises",
3+
"description": "Exercism exercises in Javascript.",
4+
"author": "Katrina Owen",
5+
"private": true,
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/exercism/javascript"
9+
},
10+
"devDependencies": {
11+
"@babel/cli": "^7.13.14",
12+
"@babel/core": "^7.13.15",
13+
"@babel/plugin-syntax-bigint": "^7.8.3",
14+
"@babel/preset-env": "^7.13.15",
15+
"@types/jest": "^26.0.22",
16+
"@types/node": "^14.14.37",
17+
"babel-eslint": "^10.1.0",
18+
"babel-jest": "^26.6.3",
19+
"core-js": "^3.10.1",
20+
"eslint": "^7.23.0",
21+
"eslint-plugin-import": "^2.22.1",
22+
"jest": "^26.6.3"
23+
},
24+
"scripts": {
25+
"test": "jest --no-cache ./*",
26+
"watch": "jest --no-cache --watch ./*",
27+
"lint": "eslint ."
28+
},
29+
"license": "MIT",
30+
"dependencies": {}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// This is only a SKELETON file for the 'Pascals Triangle' exercise. It's been provided as a
3+
// convenience to get you started writing code faster.
4+
//
5+
6+
export const promisify = () => {
7+
throw new Error('Remove this statement and implement this function');
8+
};
9+
10+
export const all = () => {
11+
throw new Error('Remove this statement and implement this function');
12+
};
13+
14+
export const allSettled = () => {
15+
throw new Error('Remove this statement and implement this function');
16+
};
17+
18+
export const race = () => {
19+
throw new Error('Remove this statement and implement this function');
20+
};
21+
22+
export const any = () => {
23+
throw new Error('Remove this statement and implement this function');
24+
};

0 commit comments

Comments
 (0)