Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week2-01 hard problems #1164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions week-2/01-async-js/hard (promises)/1-promisify-setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/

function wait(n) {
return new Promise((resolve) => {
setTimeout((_) => {
resolve();
}, n * 1000);
});
}

module.exports = wait;
3 changes: 3 additions & 0 deletions week-2/01-async-js/hard (promises)/2-sleep-completely.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/

function sleep(milliseconds) {
return new Promise((res) => {
setTimeout(res, milliseconds);
});
}

module.exports = sleep;
17 changes: 13 additions & 4 deletions week-2/01-async-js/hard (promises)/3-promise-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@
*/

function wait1(t) {

return new Promise((res) => {
setTimeout(res, t * 1000);
});
}

function wait2(t) {

return new Promise((res) => {
setTimeout(res, t * 1000);
});
}

function wait3(t) {

return new Promise((res) => {
setTimeout(res, t * 1000);
});
}

function calculateTime(t1, t2, t3) {

const start = Date.now();
return Promise.all([wait1(t1), wait2(t2), wait3(t3)]).then(
() => Date.now() - start
);
}

module.exports = calculateTime;
20 changes: 16 additions & 4 deletions week-2/01-async-js/hard (promises)/4-promise-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@
* Compare it with the results from 3-promise-all.js
*/

function wait1(t) {
const wait = require("./1-promisify-setTimeout");

function wait1(t) {
return new Promise((res) => {
setTimeout(res, t * 1000);
});
}

function wait2(t) {

return new Promise((res) => {
setTimeout(res, t * 1000);
});
}

function wait3(t) {

return new Promise((res) => {
setTimeout(res, t * 1000);
});
}

function calculateTime(t1, t2, t3) {

const start = Date.now();
return wait1(t1)
.then(() => wait2(t2))
.then(() => wait3(t3))
.then(() => Date.now() - start);
}

module.exports = calculateTime;