event.respondWith()会决定如何响应fetch事件。caches.match()查询请求然后返回之前创建的缓存中的任意缓存数据并返回 promise。 如果有,则返回该缓存数据。 否则,执行fetch。 检查返回的状态码是否是200。同时检查响应类型是否为 basic,即检查请求是否同域。当前场景不缓存第三方资源的请求。 把返回数据添加到缓存中。
=='string'){returnPromise.reject(newTypeError('must specify a string'));}returnnewPromise(function(resolve,reject){constimage=newImage();// 允许 canvas 跨域加载图片image.crossOrigin="anonymous";image.onload=function(){const$
varCACHE_NAME='my-web-app-cache';varurlsToCache = ['/','/styles/main.css','/scripts/app.js','/scripts/lib.js']; self.addEventListener('install',function(event) {// event.waitUntil 使用 promise 来获得安装时长及安装是否失败event.waitUntil( caches.open(CACHE_NAME) .then(function(cache)...
DOCTYPEhtml>Canvas to base64functionloadImageAsync(url) {if(typeofurl !=='string') {returnPromise.reject(newTypeError('must specify a string')); }returnnewPromise(function(resolve, reject) {constimage =newImage();// 允许 canvas 跨域加载图片image.crossOrigin="anonymous"; image.onload=function()...
A promise is a thenable object, and so it takes the message then, or in other words, some code can invoke the then method on this object, which is part of the interface, with a fulfillment handler, which is "the next step to take", and a rejection handler, which is "the ne...
The async and await keywords enable asynchronous, promise-based behavior in a cleaner style. Let’s understand how async/await works. The function we are waiting for should return an instance of Promise class to wait for it to execute using the await keyword before calling it. As mentioned ab...
(Note: Promises are objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value. In the case of Fetch API, it returns a Promise that resolves the response of the request.) The Fetch API is a game-changer for developers, giving them unparal...
即使async函数内部没有await调用,它也始终返回一个 Promise。 该 Promise 将使用函数返回的值解析。 如果函数引发错误,则 Promise 将被拒绝,并且会返回抛出的值。 承诺 因为嵌套的回调可能难以读取和管理,所以 Node.js 添加了对承诺的支持。 承诺是表示异步操作最终完成(或失败)的对象。
startsWith('text/html') === false) return response; //Only parse html body let { readable, writable } = new TransformStream(); let promise = injectScripts(response.body, writable); return new Response(readable, response); } let encoder = new TextEncoder('utf-8'); ...
It is up to you to craft an HTML layout that works on a printed page. As far as I know, print.js does not do this for you. I recommend reading the documentation to make sure the API fits your needs. You can also read the source code which is probably the best way to understand...