Skip to content

Instantly share code, notes, and snippets.

@gotoweb
Created June 26, 2019 08:46
Show Gist options
  • Save gotoweb/761143581e60cbf7af78ac9c1ac60b9a to your computer and use it in GitHub Desktop.
Save gotoweb/761143581e60cbf7af78ac9c1ac60b9a to your computer and use it in GitHub Desktop.
ordering-coffee-async.js
function waitAsync(callback, ms) {
setTimeout(callback, ms); // 특정 시간 이후에 callback 함수가 실행되게끔 하는 브라우저 내장 기능입니다
}
function drink(person, coffee) {
console.log(person + '는 ' + coffee + '를 마십니다');
}
function orderCoffeeSync(coffee) {
console.log(coffee + '가 접수되었습니다');
waitSync(2000);
return coffee;
}
let customers = [{
name: 'Steve',
request: '카페라떼'
}, {
name: 'John',
request: '아메리카노'
}];
function orderCoffeeAsync(menu, callback) {
console.log(menu + '가 접수되었습니다');
waitAsync(function() {
callback(menu);
}, 2000);
}
// call asynchronously
customers.forEach(function(customer) {
orderCoffeeAsync(customer.request, function(coffee) {
drink(customer.name, coffee);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment