Created
May 7, 2019 09:46
-
-
Save gotoweb/b198ff98d2571cee411567f49fe38815 to your computer and use it in GitHub Desktop.
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 wait(ms) { | |
var start = Date.now(); | |
var now = start; | |
while (now - start < ms) { | |
now = Date.now(); | |
} | |
} | |
function drink(person, coffee) { | |
console.log(person + '는 <' + coffee + '>를 마십니다'); | |
} | |
function orderCoffeeSync(menu) { | |
console.log(menu + '가 접수되었습니다'); | |
wait(2000); | |
return '코드벅스 ' + menu; | |
} | |
function orderCoffeeAsync(menu, callback) { | |
console.log(menu + '가 접수되었습니다'); | |
setTimeout(function() { | |
callback('코드벅스 ' + menu); | |
}, 2000); | |
} | |
let customers = [{ | |
name: 'steve', | |
whatIwant: '카페라떼' | |
}, { | |
name: 'johnny', | |
whatIwant: '아메리카노' | |
}]; | |
// call synchronously | |
customers.forEach(function(customer) { | |
let coffee = orderCoffeeSync(customer.whatIwant); | |
drink(customer.name, coffee); | |
}); | |
// call asynchronously | |
customers.forEach(function(customer) { | |
orderCoffeeAsync(customer.whatIwant, function(coffee) { | |
drink(customer.name, coffee) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment