{ "data": { "question": { "questionId": "2733", "questionFrontendId": "2621", "boundTopicId": null, "title": "Sleep", "titleSlug": "sleep", "content": "

Given a positive integer millis, write an asynchronous function that sleeps for millis milliseconds. It can resolve any value.

\n\n

 

\n

Example 1:

\n\n
\nInput: millis = 100\nOutput: 100\nExplanation: It should return a promise that resolves after 100ms.\nlet t = Date.now();\nsleep(100).then(() => {\n  console.log(Date.now() - t); // 100\n});\n
\n\n

Example 2:

\n\n
\nInput: millis = 200\nOutput: 200\nExplanation: It should return a promise that resolves after 200ms.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 514, "dislikes": 38, "isLiked": null, "similarQuestions": "[{\"title\": \"Promise Time Limit\", \"titleSlug\": \"promise-time-limit\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Promise Pool\", \"titleSlug\": \"promise-pool\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "exampleTestcases": "100\n200", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} millis\n * @return {Promise}\n */\nasync function sleep(millis) {\n \n}\n\n/** \n * let t = Date.now()\n * sleep(100).then(() => console.log(Date.now() - t)) // 100\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "async function sleep(millis: number): Promise {\n \n}\n\n\n/** \n * let t = Date.now()\n * sleep(100).then(() => console.log(Date.now() - t)) // 100\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"80.8K\", \"totalSubmission\": \"94.9K\", \"totalAcceptedRaw\": 80796, \"totalSubmissionRaw\": 94900, \"acRate\": \"85.1%\"}", "hints": [ "In Javascript, you can execute code after some delay with the setTimeout(fn, sleepTime) function.", "An async function is defined as function which returns a Promise.", "To create a Promise, you can code new Promise((resolve, reject) => {}). When you want the function to return a value, code resolve(value) inside the callback." ], "solution": { "id": "1898", "canSeeDetail": false, "paidOnly": true, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "100", "metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"millis\",\n \"type\": \"integer\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 5.1.6, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }