JavaScript और TypeScript स्निपेट
12 JavaScript स्निपेट (टाइमआउट वाला fetch, Promise.allSettled, ग्रुपिंग, localStorage) और 8 TypeScript स्निपेट (टाइप गार्ड, पूरा switch, as const, satisfies)। JavaScript वाले .js फ़ाइलों में और TypeScript वाले .ts फ़ाइलों में दिखते हैं।
मुफ़्त में इंस्टॉल करें।
अगर आपके Android डिवाइस पर PocketCode इंस्टॉल है, तो यह पेज ऐप में खुलेगा।
प्रीव्यू
20 में से 5
Fetch JSON with an error checkfetchj
Async function that throws on a non-2xx response and returns the parsed JSON
async function ${1:getData}(${2:url}) {
const res = await fetch(${2:url});
if (!res.ok) throw new Error("HTTP " + res.status);
return res.json();
}Fetch with a timeoutfetcht
Abort a request that takes too long
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), ${1:5000});
try {
const res = await fetch(${2:url}, { signal: controller.signal });
$0
} finally {
clearTimeout(timer);
}Sleepsleep
Promise that resolves after a number of milliseconds
const ${1:sleep} = (ms) => new Promise((resolve) => setTimeout(resolve, ms));Run promises in parallelpall
Await several promises at once and destructure the results
const [${1:a}, ${2:b}] = await Promise.all([${3:loadA()}, ${4:loadB()}]);Settle all promisespsettled
Wait for every promise and handle successes and failures one by one
const results = await Promise.allSettled(${1:tasks});
for (const r of results) {
if (r.status === "fulfilled") {
${2:console.log(r.value);}
} else {
${3:console.error(r.reason);}
}
}1.0.0 में बदलाव
पहला संस्करण।