Last active
May 29, 2020 09:33
-
-
Save will-yama/049f3cccfc9a4363109016489a51750c to your computer and use it in GitHub Desktop.
Introduction to Promises
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 PrintWordA(word) { | |
console.log(word); | |
} | |
function PrintWordB(word) { | |
setTimeout(function() { | |
console.log(word); | |
}, 1000); | |
} | |
function PrintWordC(word) { | |
console.log(word); | |
} | |
PrintWordA('Piece'); | |
PrintWordB('of'); | |
PrintWordC('Cake.'); |
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 money = 1000; | |
function minusA(num) { | |
money = money - num; | |
if (money > 0) { | |
console.log('Process A finished. You have $' + money + ' remaining.'); | |
} else { | |
console.log('Process A finished. Your cash total is negative.'); | |
} | |
} | |
function minusB(num) { | |
setTimeout(function() { | |
money = money - num; | |
if (money > 0) { | |
console.log('Process B finished. You have $' + money + ' remaining.'); | |
} else { | |
console.log('Process B finished. Your cash total is negative.'); | |
} | |
}, 1000); | |
} | |
function minusC(num) { | |
money = money - num; | |
if (money > 0) { | |
console.log('Process C finished. You have $' + money + ' remaining.'); | |
} else { | |
console.log('Process C finished. Your cash total is negative.'); | |
} | |
} | |
minusA(600); | |
minusB(500); | |
minusC(200); |
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 money = 1000; | |
function minusA(num) { | |
money = money - num; | |
if (money > 0) { | |
console.log('Process A finished. You have $' + money + ' remaining'); | |
} else { | |
console.log('Process A finished. Your cash total is negative.'); | |
} | |
} | |
function minusB(num, func) { | |
setTimeout(function() { | |
money = money - num; | |
if (money > 0) { | |
console.log('Process B finished. You have $' + money + ' remaining'); | |
} else { | |
console.log('Process B finished. Your cash total is negative.'); | |
} | |
func(200); | |
}, 1000); | |
} | |
function minusC(num) { | |
money = money - num; | |
if (money > 0) { | |
console.log('Process C finished. You have $' + money + ' remaining'); | |
} else { | |
console.log('Process C finished. Your cash total is negative.'); | |
} | |
} | |
minusA(600); | |
minusB(500, minusC); |
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 money = 1000; | |
function minusA(num) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
money = money - num; | |
if (money > 0) { | |
resolve('Process A finished. You have $' + money + ' remaining.'); | |
} else { | |
reject('Your cash total is negative'); | |
} | |
}, 1000); | |
}); | |
} | |
function minusB(num) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
money = money - num; | |
if (money > 0) { | |
resolve('Process B finished. You have $' + money + ' remaining.'); | |
} else { | |
reject('Your cash total is negative'); | |
} | |
}, 1000); | |
}); | |
} | |
function minusC(num) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
money = money - num; | |
if (money > 0) { | |
resolve('Process C finished. You have $' + money + ' remaining.'); | |
} else { | |
reject('Your cash total is negative'); | |
} | |
}, 1000); | |
}); | |
} | |
minusA(600).then(function (resultA) { | |
console.log(resultA); | |
return minusB(500); | |
}).then(function (resultB) { | |
console.log(resultB); | |
return minusC(400); | |
}).then(function (resultC) { | |
console.log(resultC); | |
}).catch(function (error) { | |
console.log(error); | |
}); |
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 () { | |
'use strict'; | |
window.alert('Hello kintone'); | |
})(); |
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 () { | |
'use strict'; | |
var SWAPIendpoint = 'https://swapi.co/api/people/?'; | |
var SWAPIparams = 'search=Luke'; | |
var SWAPIurl = SWAPIendpoint + SWAPIparams; | |
// Star Wars API call | |
var getStarWarsCharacterWithPromise = function () { | |
// Get Character Details | |
return new kintone.Promise(function (resolve, reject) { | |
kintone.proxy(SWAPIurl, 'GET', {}, {}, function (resp) { | |
var jsonResp = JSON.parse(resp).results[0]; | |
console.log(jsonResp); | |
resolve(jsonResp); | |
}, function (err) { | |
console.log(err); | |
reject(err); | |
}); | |
}).then(function (resp) { | |
// Get Homeworld details | |
return new kintone.Promise(function (resolve, reject) { | |
kintone.proxy(resp.homeworld, 'GET', {}, {}, function (respHomeworld) { | |
var jsonResp = JSON.parse(respHomeworld); | |
console.log(jsonResp); | |
resolve(resp); | |
}, function (err) { | |
console.log(err); | |
}); | |
}); | |
}).then(function (resp) { | |
// Get Species details | |
return new kintone.Promise(function (resolve, reject) { | |
kintone.proxy(resp.species[0], 'GET', {}, {}, function (respSpecies) { | |
var jsonResp = JSON.parse(respSpecies); | |
console.log(jsonResp); | |
resolve(resp); | |
}, function (err) { | |
console.log(err); | |
}); | |
}); | |
}); | |
}; | |
// Create a button on the Kintone Record List page | |
kintone.events.on('app.record.index.show', function (event) { | |
var button = document.createElement('button'); | |
button.textContent = 'Click Me!'; | |
kintone.app.getHeaderMenuSpaceElement().appendChild(button); | |
button.onclick = function () { | |
console.log('Start Search'); | |
getStarWarsCharacterWithPromise().then(function (resp) { | |
console.log('End Search'); | |
}); | |
}; | |
return event; | |
}); | |
})(); |
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 () { | |
'use strict'; | |
var SWAPIendpoint = 'https://swapi.co/api/people/?'; | |
var SWAPIparams = 'search=Luke'; | |
var SWAPIurl = SWAPIendpoint + SWAPIparams; | |
// Star Wars API call | |
var getStarWarsCharacterWithPromise = function () { | |
return new kintone.Promise(function (resolve, reject) { | |
kintone.proxy(SWAPIurl, 'GET', {}, {}, function (resp) { | |
var jsonResp = JSON.parse(resp).results[0]; | |
console.log(jsonResp) | |
resolve(); | |
}, function (err) { | |
console.log(err); | |
reject(err); | |
}); | |
}); | |
}; | |
// Create a button on the Kintone Record List page | |
kintone.events.on('app.record.index.show', function (event) { | |
var button = document.createElement('button'); | |
button.textContent = 'Click Me!'; | |
kintone.app.getHeaderMenuSpaceElement().appendChild(button); | |
button.onclick = function () { | |
console.log('Start Search'); | |
getStarWarsCharacterWithPromise().then(function () { | |
console.log('End Search'); | |
}); | |
}; | |
return event; | |
}); | |
})(); |
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 () { | |
'use strict'; | |
var currencyapiEndpoint = 'https://currencyapi.net/api/v1/rates?key=demo'; | |
// Currency API call | |
kintone.proxy(currencyapiEndpoint, 'GET', {}, {}, function (resp) { | |
var jsonResp = JSON.parse(resp); | |
console.log(jsonResp); | |
}, function(error) { | |
console.log(error); | |
}); | |
})(); |
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 url = 'https://currencyapi.net/api/v1/rates?key=demo'; | |
fetch( url ) | |
.then( function( data ) { | |
return data.json(); | |
}) | |
.then( function( json ) { | |
console.log(json); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment