This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let peserta1 = new Promise(resolve => setTimeout(resolve, 30, 'Peserta 1')) | |
let peserta2 = new Promise((resolve,reject) => setTimeout(reject, 20, 'Peserta 2')) | |
let peserta3 = new Promise(resolve => setTimeout(resolve, 50, 'Peserta 3')) | |
let peserta4 = new Promise(resolve => setTimeout(resolve, 100, 'Peserta 4')) | |
let peserta5 = new Promise(resolve => setTimeout(resolve, 90, 'Peserta 5')) | |
Promise.race([peserta1, peserta2, peserta3, peserta4, peserta5]) | |
.then(val => console.log('Balapan selesai,Pemenangnya adalah:', val)) | |
.catch(err => console.log('Balapan dihentikan karena : ', err)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let peserta1 = new Promise(resolve => setTimeout(resolve, 30, 'Peserta 1.')) | |
let peserta2 = new Promise(resolve => setTimeout(resolve, 20, 'Peserta 2.')) | |
let peserta3 = new Promise(resolve => setTimeout(resolve, 50, 'Peserta 3.')) | |
let peserta4 = new Promise(resolve => setTimeout(resolve, 100, 'Peserta 4.')) | |
let peserta5 = new Promise(resolve => setTimeout(resolve, 90, 'Peserta 5.')) | |
Promise.race([peserta1, peserta2, peserta3, peserta4, peserta5]) | |
.then(val => console.log('Balapan selesai,Pemenangnya adalah:', val)) | |
.catch(err => console.log('Balapan dihentikan karena : ', err)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getPost = () => fetch('https://jsonplaceholder.typicode.com/posts/1') | |
const getAuthor = (id) => fetch('https://jsonplaceholder.typicode.com/users/' + id) | |
const getComment = (id) => fetch('https://jsonplaceholder.typicode.com/users/' + id) | |
var a = getPost().then(res => res.json()) // #1 get post | |
var b = a.then(res => getAuthor(res.id)).then(res => res.json()) // #2 get author | |
var c = a.then(res => getComment(res.id)).then(res => res.json()) //#3 get comment | |
Promise.all([a,b,c]).then(results => { | |
console.log(results[0]) | |
console.log(results[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getPost = () => fetch('https://jsonplaceholder.typicode.com/posts/1') | |
const getAuthor = (id) => fetch('https://jsonplaceholder.typicode.com/users/' + id) | |
const getComment = (id) => fetch('https://jsonplaceholder.typicode.com/users/' + id) | |
var a = getPost().then(res => res.json()) // #1 get post | |
var b = a.then(res => getAuthor(res.id)).then(res => res.json()) // #2 get author | |
var c = a.then(res => getComment(res.id)).then(res => res.json()) //#3 get comment | |
Promise.all([a,b,c]).then(results => { | |
console.log(results[0]) | |
console.log(results[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getPost = () => fetch('https://jsonplaceholder.typicode.com/posts/1') | |
const getAuthor = (id) => fetch('https://jsonplaceholder.typicode.com/users/' + id) | |
const getComment = (id) => fetch('https://jsonplaceholder.typicode.com/users/' + id) | |
getPost() // #1.fetch post | |
.then(postResponse => postResponse.json()) // #2. get & return post json | |
.then(postResponse => getAuthor(postResponse.id) // #3. fetch author | |
.then(authorResponse => authorResponse.json() // #4 get & return author json | |
.then(authorResponse => getComment(postResponse.id) // #5 fetch comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
readFileContent("a.md", function (a){ | |
readFileContent("b.md", function (b){ | |
readFileContent("b.md", function (b){ | |
writeFileContent("result.md", a + b + c, function(){ | |
console.log("we are done"); | |
}) | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = readFileContent("a.md"); | |
var b = readFileContent("b.md"); | |
var c = readFileContent("c.md"); | |
writeFileContent("result.md", a + b + c); | |
console.log("we are done"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function readFileCallback(err,data){ | |
if (err){ | |
console.log('Error Read File :' + err); | |
}else{ | |
console.log(data.toString()) | |
} | |
} | |
var data = fs.readFile('hello.md',readFileCallback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
var data = fs.readFileSync('hello.md') | |
console.log('Read File Done :' + data.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function p1() { | |
console.log('p1 done') | |
} | |
function p2(callback) { | |
setTimeout( | |
function() { | |
console.log('p2 done') | |
callback() |