Difference between revisions of "Bot Playground/Built-in Functions/process.sleep"
From SmartBots Developers Docs
m (Gg moved page Bot Playground/Built-in Functions/sleep to Bot Playground/Built-in Functions/process.sleep) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
− | sleep(timeMs); | + | await process.sleep(timeMs); |
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === Note === | ||
+ | |||
+ | Previously known as bare "sleep()". | ||
{{API Command Table}} | {{API Command Table}} | ||
Line 19: | Line 23: | ||
== Comments == | == Comments == | ||
− | This function can be used to delay the code execution for a specific time. Note that this function is asynchronous (like the most of javascript), so can't write "doSomething(); sleep(1000); doAfterDelay;". See the examples below. | + | <del>This function can be used to delay the code execution for a specific time. Note that this function is asynchronous (like the most of javascript), so can't write "doSomething(); process.sleep(1000); doAfterDelay;". See the examples below.</del> |
+ | |||
+ | The process.sleep() function immediately returns a Promise. You can use [[Bot_Playground/Async_and_await|async/await]] to pause your script (see examples below). | ||
== Examples == | == Examples == | ||
− | The function can be used in | + | The function can be used in three ways: |
− | 1. | + | 1. Async/await (recommended) |
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
console.log("one1"); | console.log("one1"); | ||
− | sleep(2000) | + | await process.sleep(2000); |
+ | |||
+ | console.log("two2"); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 2. Standalone: | ||
+ | |||
+ | <syntaxhighlight lang="javascript"> | ||
+ | console.log("one1"); | ||
+ | |||
+ | process.sleep(2000) | ||
.then(function() { | .then(function() { | ||
console.log("two2"); | console.log("two2"); | ||
Line 36: | Line 52: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 3. Within the chain: | |
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
+ | // SmartBots Playground code start v1.0 (automatic line) | ||
http.get("https://mysmartbots.com") | http.get("https://mysmartbots.com") | ||
.then(function() { | .then(function() { | ||
console.log("one"); | console.log("one"); | ||
− | return sleep(2000); | + | return process.sleep(2000); |
}) | }) | ||
.then(function() { | .then(function() { | ||
console.log("two"); | console.log("two"); | ||
+ | }) | ||
+ | |||
+ | .then(function() { | ||
+ | // Gracefully stop the test script | ||
+ | exit(); | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 20:49, 6 October 2022
Pauses a program execution.
await process.sleep(timeMs);
Note
Previously known as bare "sleep()".
Reference
This command accepts the following parameters:
Variable | Required | Description
| |
---|---|---|---|
Input: | |||
timeMs | yes | sleep time, ms
| |
Output: | |||
Function returns a Promise with the following data: | |||
success | bool | true if command completed successfully | |
error | string | error string if command has failed |
Comments
This function can be used to delay the code execution for a specific time. Note that this function is asynchronous (like the most of javascript), so can't write "doSomething(); process.sleep(1000); doAfterDelay;". See the examples below.
The process.sleep() function immediately returns a Promise. You can use async/await to pause your script (see examples below).
Examples
The function can be used in three ways:
1. Async/await (recommended)
console.log("one1");
await process.sleep(2000);
console.log("two2");
2. Standalone:
console.log("one1");
process.sleep(2000)
.then(function() {
console.log("two2");
});
3. Within the chain:
// SmartBots Playground code start v1.0 (automatic line)
http.get("https://mysmartbots.com")
.then(function() {
console.log("one");
return process.sleep(2000);
})
.then(function() {
console.log("two");
})
.then(function() {
// Gracefully stop the test script
exit();
});