Difference between revisions of "Bot Playground/Async and await"
From SmartBots Developers Docs
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:{{SUBPAGENAME}}}} | {{DISPLAYTITLE:{{SUBPAGENAME}}}} | ||
+ | == TL;DR == | ||
+ | |||
+ | This is how async/await Playground script looks like: | ||
+ | |||
+ | <syntaxhighlight lang="javascript"> | ||
+ | const AVATAR = "Smartbots Resident"; | ||
+ | |||
+ | console.log("The age is:", await getAge(AVATAR); | ||
+ | |||
+ | async function getAge(slname) { | ||
+ | const res1 = await Bot.name2key(slname); | ||
+ | const res2 = await Bot.avatarProfile(res1.slkey); | ||
+ | return res2.age; | ||
+ | } | ||
+ | |||
+ | console.log("This log appears only after all calculations are complete"); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == A long story == | ||
Async/await is the JavaScript modern approach to the asynchronous programming. The concept may be hard to understand at the very beginning, but you'll find async functions extremely useful once you get used to them. | Async/await is the JavaScript modern approach to the asynchronous programming. The concept may be hard to understand at the very beginning, but you'll find async functions extremely useful once you get used to them. | ||
− | Let's take an example from [[/Callbacks and return values|Callbacks and return values]] page: | + | Let's take an example from [[../Callbacks and return values|Callbacks and return values]] page: |
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
Line 26: | Line 45: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | After rewriting using ''await'' the code looks this way: | |
− | + | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
Line 49: | Line 67: | ||
== Notes == | == Notes == | ||
− | Remember that ''await'' may appear in ''async'' functions only (see JavaScript reference and tutorials for understanding). Thus, you may need to convert all your functions to async: | + | Remember that ''await'' may appear in ''async'' functions only (see [https://javascript.info/async-await JavaScript reference] and various tutorials for understanding). Thus, you may need to convert all your functions to async: |
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> |
Latest revision as of 17:54, 6 October 2022
TL;DR
This is how async/await Playground script looks like:
const AVATAR = "Smartbots Resident";
console.log("The age is:", await getAge(AVATAR);
async function getAge(slname) {
const res1 = await Bot.name2key(slname);
const res2 = await Bot.avatarProfile(res1.slkey);
return res2.age;
}
console.log("This log appears only after all calculations are complete");
A long story
Async/await is the JavaScript modern approach to the asynchronous programming. The concept may be hard to understand at the very beginning, but you'll find async functions extremely useful once you get used to them.
Let's take an example from Callbacks and return values page:
var slname = "Smartbots Resident";
Bot.name2key(slname)
.then(function(result) {
// this code will be called when name2key() get completed
console.log("Got the UUID!", result.slkey);
// Oh, now the next step
return Bot.avatarProfile(result.slkey);
})
.then(function(result) {
// this code will be called when avatarProfile() get completed
console.log("Got the age!", result2.age);
Bot.im("Glaznah Gassner", "The age of " + slname + " is " + result2.age);
});
console.log("I've asked for UUID and now waiting for answer");
After rewriting using await the code looks this way:
const slname = "Smartbots Resident";
const res1 = await Bot.name2key(slname);
// this code will be called when name2key() get completed
console.log("Got the UUID!", res1.slkey);
const res2 = await Bot.avatarProfile(res1.slkey);
// this code will be called when avatarProfile() get completed
console.log("Got the age!", res2.age);
await Bot.im("Glaznah Gassner", `The age of ${slname} is ${res2.age}`);
console.log("The script has been completed");
Looks much simpler and easier!
Notes
Remember that await may appear in async functions only (see JavaScript reference and various tutorials for understanding). Thus, you may need to convert all your functions to async:
const AVATAR = "Smartbots Resident";
console.log("The age is:", await getAge(AVATAR);
async function getAge(slname) {
const res1 = await Bot.name2key(slname);
const res2 = await Bot.avatarProfile(res1.slkey);
return res2.age;
}
More examples
Check Bot Playground Examples for more code to play with.