JSFiddle - React, Tailwind, and code Playground
JavaScript
// Challenge:
// 1. modify AnticipatedSyncFunction to produce the expected
// output.
// 2. AnticipatedSyncFunction has to call async function setTimeout
// to populate ret variable.
// 3. ret variable can only be set by the callback of setTimeout.
// 4. No change to code outside function AnticipatedSyncFunction.
function AnticipatedSyncFunction(){
var ret;
setTimeout(function(){
ret = "hello";
},3000);
return ret;
}
var output = AnticipatedSyncFunction();
//expected: output=hello
alert("output="+output);
// actual: output=undefined