How to use promises method in wpt

Best JavaScript code snippet using wpt

promise.js

Source:promise.js Github

copy

Full Screen

1define(function(require, exports, module) {2 /*!3 ** Thenable -- Embeddable Minimum Strictly-Compliant Promises/A+ 1.1.1 Thenable4 ** Copyright (c) 2013-2014 Ralf S. Engelschall <http://engelschall.com>5 ** Licensed under The MIT License <http://opensource.org/licenses/MIT>6 ** Source-Code distributed on <http://github.com/rse/thenable>7 */8 /* promise states [Promises/A+ 2.1] */9 var STATE_PENDING = 0; /* [Promises/A+ 2.1.1] */10 var STATE_FULFILLED = 1; /* [Promises/A+ 2.1.2] */11 var STATE_REJECTED = 2; /* [Promises/A+ 2.1.3] */12 /* promise object constructor */13 var Promise = function(executor) {14 /* optionally support non-constructor/plain-function call */15 if (!(this instanceof Promise))16 return new Promise(executor);17 /* initialize object */18 this.id = 'Thenable/1.0.7';19 this.state = STATE_PENDING; /* initial state */20 this.fulfillValue = undefined; /* initial value */ /* [Promises/A+ 1.3, 2.1.2.2] */21 this.rejectReason = undefined; /* initial reason */ /* [Promises/A+ 1.5, 2.1.3.2] */22 this.onFulfilled = []; /* initial handlers */23 this.onRejected = []; /* initial handlers */24 /* support optional executor function */25 if (typeof executor === 'function')26 executor.call(this, this.fulfill.bind(this), this.reject.bind(this));27 };28 /* Promise API methods */29 Promise.prototype = {30 /* promise resolving methods */31 fulfill: function(value) { return deliver(this, STATE_FULFILLED, 'fulfillValue', value); },32 reject: function(value) { return deliver(this, STATE_REJECTED, 'rejectReason', value); },33 /* 'The then Method' [Promises/A+ 1.1, 1.2, 2.2] */34 then: function(onFulfilled, onRejected) {35 var curr = this;36 var next = new Promise(); /* [Promises/A+ 2.2.7] */37 curr.onFulfilled.push(38 resolver(onFulfilled, next, 'fulfill')); /* [Promises/A+ 2.2.2/2.2.6] */39 curr.onRejected.push(40 resolver(onRejected, next, 'reject')); /* [Promises/A+ 2.2.3/2.2.6] */41 execute(curr);42 return next; /* [Promises/A+ 2.2.7, 3.3] */43 }44 };45 Promise.all = function (arr) {46 return new Promise(function(resolve, reject) {47 var len = arr.length,48 i = 0,49 res = 0,50 results = [];51 if (len === 0) {52 resolve(results);53 }54 while (i < len) {55 arr[i].then(56 function (result) {57 results.push(result);58 if (++res === len) {59 resolve(results);60 }61 },62 function (val) {63 reject(val);64 }65 );66 i++;67 }68 });69 };70 /* deliver an action */71 var deliver = function(curr, state, name, value) {72 if (curr.state === STATE_PENDING) {73 curr.state = state; /* [Promises/A+ 2.1.2.1, 2.1.3.1] */74 curr[name] = value; /* [Promises/A+ 2.1.2.2, 2.1.3.2] */75 execute(curr);76 }77 return curr;78 };79 /* execute all handlers */80 var execute = function(curr) {81 if (curr.state === STATE_FULFILLED)82 execute_handlers(curr, 'onFulfilled', curr.fulfillValue);83 else if (curr.state === STATE_REJECTED)84 execute_handlers(curr, 'onRejected', curr.rejectReason);85 };86 /* execute particular set of handlers */87 var execute_handlers = function(curr, name, value) {88 /* global process: true */89 /* global setImmediate: true */90 /* global setTimeout: true */91 /* short-circuit processing */92 if (curr[name].length === 0)93 return;94 /* iterate over all handlers, exactly once */95 var handlers = curr[name];96 curr[name] = []; /* [Promises/A+ 2.2.2.3, 2.2.3.3] */97 var func = function() {98 for (var i = 0; i < handlers.length; i++)99 handlers[i](value); /* [Promises/A+ 2.2.5] */100 };101 /* execute procedure asynchronously */ /* [Promises/A+ 2.2.4, 3.1] */102 if (typeof process === 'object' && typeof process.nextTick === 'function')103 process.nextTick(func);104 else if (typeof setImmediate === 'function')105 setImmediate(func);106 else107 setTimeout(func, 0);108 };109 /* generate a resolver function */110 var resolver = function(cb, next, method) {111 return function(value) {112 if (typeof cb !== 'function') /* [Promises/A+ 2.2.1, 2.2.7.3, 2.2.7.4] */113 next[method].call(next, value); /* [Promises/A+ 2.2.7.3, 2.2.7.4] */114 else {115 var result;116 try {117 if (value instanceof Promise) {118 result = value.then(cb);119 }120 else result = cb(value);121 } /* [Promises/A+ 2.2.2.1, 2.2.3.1, 2.2.5, 3.2] */122 catch (e) {123 next.reject(e); /* [Promises/A+ 2.2.7.2] */124 return;125 }126 resolve(next, result); /* [Promises/A+ 2.2.7.1] */127 }128 };129 };130 /* 'Promise Resolution Procedure' */ /* [Promises/A+ 2.3] */131 var resolve = function(promise, x) {132 /* sanity check arguments */ /* [Promises/A+ 2.3.1] */133 if (promise === x) {134 promise.reject(new TypeError('cannot resolve promise with itself'));135 return;136 }137 /* surgically check for a 'then' method138 (mainly to just call the 'getter' of 'then' only once) */139 var then;140 if ((typeof x === 'object' && x !== null) || typeof x === 'function') {141 try { then = x.then; } /* [Promises/A+ 2.3.3.1, 3.5] */142 catch (e) {143 promise.reject(e); /* [Promises/A+ 2.3.3.2] */144 return;145 }146 }147 /* handle own Thenables [Promises/A+ 2.3.2]148 and similar 'thenables' [Promises/A+ 2.3.3] */149 if (typeof then === 'function') {150 var resolved = false;151 try {152 /* call retrieved 'then' method */ /* [Promises/A+ 2.3.3.3] */153 then.call(x,154 /* resolvePromise */ /* [Promises/A+ 2.3.3.3.1] */155 function(y) {156 if (resolved) return; resolved = true; /* [Promises/A+ 2.3.3.3.3] */157 if (y === x) /* [Promises/A+ 3.6] */158 promise.reject(new TypeError('circular thenable chain'));159 else160 resolve(promise, y);161 },162 /* rejectPromise */ /* [Promises/A+ 2.3.3.3.2] */163 function(r) {164 if (resolved) return; resolved = true; /* [Promises/A+ 2.3.3.3.3] */165 promise.reject(r);166 }167 );168 }169 catch (e) {170 if (!resolved) /* [Promises/A+ 2.3.3.3.3] */171 promise.reject(e); /* [Promises/A+ 2.3.3.3.4] */172 }173 return;174 }175 /* handle other values */176 promise.fulfill(x); /* [Promises/A+ 2.3.4, 2.3.3.4] */177 };178 Promise.resolve = function(value) {179 return new Promise(function(resolve) {180 resolve(value);181 });182 };183 Promise.reject = function(reason) {184 return new Promise(function(resolve, reject) {185 reject(reason);186 });187 };188 /* export API */189 module.exports = Promise;...

Full Screen

Full Screen

created-promises.js

Source:created-promises.js Github

copy

Full Screen

1$("#btn-created-promises").click(function () {2 $("#table-created-promises").toggle("slow");3});4$("#btn-more-created-promises").click(function () {5 user = new Object();6 user.email = $(".user-email").text();7 user.offset = $("#table-created-promises tbody tr").length;8 getMoreCreatedPromises(user);9});10function getMoreCreatedPromises(user) {11 $("#btn-more-created-promises").hide();12 $("#table-created-promises .processing-image").show();13 var userJSON = JSON.stringify(user);14 $.ajax({15 type: "POST",16 url: getMoreCreatedPromisesUrl,17 data: {user: userJSON},18 success: function (data, txtStatus, jqXHR) {19 var json = JSON.parse(data);20 if (json.success) {21 fillTableCreatedPromises(json);22 }23 },24 fail: function (data, txtStatus, jqXHR) {25 console.log(txtStatus);26 console.log("fail");27 $("#table-created-promises .alert-danger").text("Error inesperado, prueba mas tarde.");28 $("#table-created-promises").find(".processing-image").hide();29 },30 error: function (data, txtStatus, jqXHR) {31 var json = JSON.parse(data.responseText);32 if (json.error) {33 $("#table-created-promises .alert-danger").text(json.error);34 }35 },36 complete: function () {37 $("#table-created-promises").find(".processing-image").hide();38 }39 });40}41function fillTableCreatedPromises(json) {42 if (json.nextCreatedPromises === "more" || json.nextCreatedPromises === "no-more")43 {44 $("#btn-created-promises").show();45 $(".panel-body hr").show();46 if (json.createdPromises) {47 for (i = 0; i < json.createdPromises.length; i++)48 {49 var description = json.createdPromises[i]["description"];50 var action = json.createdPromises[i]["action"];51 var lastDateAction = json.createdPromises[i]["dateEvent"];52 var datePromise = json.createdPromises[i]["datePromise"];53 if (json.createdPromises[i]["ownerName"])54 {55 var ownerDisplay = json.createdPromises[i]["ownerName"];56 } else if (json.createdPromises[i]["ownerEmail"])57 {58 var ownerDisplay = json.createdPromises[i]["ownerEmail"];59 }60 else if (json.createdPromises[i]["ownerPhone"])61 {62 var ownerDisplay = json.createdPromises[i]["ownerPhone"];63 }64 if (json.createdPromises[i]["receiverName"])65 {66 var receiverDisplay = json.createdPromises[i]["receiverName"];67 } else if (json.createdPromises[i]["receiverEmail"])68 {69 var receiverDisplay = json.createdPromises[i]["receiverEmail"];70 }71 else if (json.createdPromises[i]["receiverPhone"])72 {73 var receiverDisplay = json.createdPromises[i]["receiverPhone"];74 }75 var ownerCode = json.createdPromises[i]["ownerCode"];76 var receiverCode = json.createdPromises[i]["receiverCode"];77 var code = json.createdPromises[i]["code"];78 //to translate to any desire language translate-actions.js79 var actionText = translateActions(action);80 if (i === 0) {81 $("#table-created-promises tbody").append("<tr class='scroll-mark'>");82 } else {83 $("#table-created-promises tbody").append("<tr>");84 }85 $("#table-created-promises tbody tr:last").append("<td ><div class='date-action'>"+datePromise+"</div><a href=promise/" + code + "><div class='description dotdotdot'>" + description + "</div></a></td>");86 if (action === "claim" || action === "review")87 {88 var userToDisplay = ownerDisplay;89 var userToDisplayCode = ownerCode;90 } else {91 var userToDisplay = receiverDisplay;92 var userToDisplayCode = receiverCode;93 }94 $("#table-created-promises tbody td:last").append("<div class='responsive-actions'><div class='date-action'>" + lastDateAction + "</div><div class='text-action'>" + actionText + userToDisplay + "</div><a href='profile?info=" + userToDisplayCode + "' > <div > <b>==> Ver perfil público</b> </div></a></td>");95 $("#table-created-promises tbody tr:last").append("<td class='actions'><div class='date-action'>" + lastDateAction + "</div><div class='text-action'>" + actionText + userToDisplay + "</div><a href='profile?info=" + userToDisplayCode + "' > <div > <b>==> Ver perfil público</b> </div></a></td>");96 if (action === "claim") {97 $("#table-created-promises tbody tr:last").addClass("table-danger");98 } else if (action === "review") {99 $("#table-created-promises tbody tr:last").addClass("table-success");100 }101 }102 $("#table-created-promises").show("slow", function () {103 $("#table-created-promises").find(".dotdotdot").dotdotdot({104 ellipsis: '... ',105 wrap: 'word',106 height: 100,107 watch: true108 });109 $("#table-created-promises").find(".description").show();110 if ($("#table-created-promises tbody tr").length > 0) {111 // $("body").animate({scrollTop: $("#table-created-promises tbody .scroll-mark:last").offset().top - 200}, 2000);112 }113 if (json.nextCreatedPromises === "more") {114 $("#count-created-promises").text("(" + $("#table-created-promises tbody tr").length + "+)");115 $("#btn-more-created-promises").show("slow");116 } else {117 $("#count-created-promises").text("(" + $("#table-created-promises tbody tr").length + ")");118 }119 });120 }121 }...

Full Screen

Full Screen

own-promises.js

Source:own-promises.js Github

copy

Full Screen

1$("#btn-own-promises").click(function () {2 $("#table-own-promises").toggle("slow");3});4$("#btn-refresh-own-promises").click(function () {5 $("#btn-refresh-own-promises").hide();6 var offset = 0;7 $("#table-own-promises tbody tr").each(function () {8 $(this).remove();9 });10 getMoreOwnPromises(offset);11});12$("#btn-more-own-promises").click(function () {13 $("#table-own-promises .alert-danger").text("");14 user = new Object();15 var offset = $("#table-own-promises tbody tr").length;16 getMoreOwnPromises(offset);17});18function getMoreOwnPromises(offset) {19 $("#btn-more-own-promises").hide();20 $("#table-own-promises .processing-image").show();21 $.ajax({22 type: "POST",23 url: getMoreOwnPromisesUrl,24 data: {offset: offset},25 success: function (data, txtStatus, jqXHR) {26 var json = JSON.parse(data);27 if (json.success) {28 fillTableOwnPromises(json)29 30 }31 },32 fail: function (data, txtStatus, jqXHR) {33 console.log(txtStatus);34 console.log("fail");35 $("#table-own-promises .alert-danger").text("Error inesperado, prueba mas tarde.");36 },37 error: function (data, txtStatus, jqXHR) {38 var json = JSON.parse(data.responseText);39 if (json.error) {40 $("#table-own-promises .alert-danger").text(json.error);41 }42 },43 complete: function () {44 $("#table-own-promises").find(".processing-image").hide();45 $("#btn-refresh-own-promises").show();46 }47 });48}49function fillTableOwnPromises(json) {50 if (json.nextOwnPromises === "more" || json.nextOwnPromises === "no-more")51 {52 $("#btn-own-promises").show();53 $("#btn-refresh-own-promises").show();54 if (json.ownPromises) {55 for (i = 0; i < json.ownPromises.length; i++)56 {57 var description = json.ownPromises[i]["description"];58 var responsibleCode = json.ownPromises[i]["responsibleCode"];59 if (json.ownPromises[i]["name"])60 {61 var responsible = json.ownPromises[i]["name"];62 } else if (json.ownPromises[i]["email"])63 {64 var responsible = json.ownPromises[i]["email"];65 }66 else if (json.ownPromises[i]["phone"])67 {68 var responsible = json.createdPromises[i]["phone"];69 }70 var action = json.ownPromises[i]["action"];71 var lastDateAction = json.ownPromises[i]["dateEvent"];72 var datePromise = json.ownPromises[i]["datePromise"];73 var code = json.ownPromises[i]["code"];74 var eventCode = json.ownPromises[i]["eventCode"];75 //to translate to any desire language translate-actions.js76 var actionText = translateActions(action);77 var table_class = "";78 var scroll_mark = "";79 if (i === 0) {80 scroll_mark = "scroll-mark";81 }82 if (action === "transfer" || action === "claim") {83 table_class = "table-danger";84 }85 if (action === "review") {86 table_class = "table-success";87 }88 $("#table-own-promises tbody").append("<tr class='" + table_class + " " + scroll_mark + " '>");89 if (action === "transfer") {90 $("#table-own-promises tbody tr:last").append("<td><a href=promise/" + code + "><div class='description dotdotdot'>" + description + "</div></a>"91 + " <div class ='date-action'>" + lastDateAction + "</div><div class='text-action'>" + actionText + " ti</div><a href=activate/" + code + "&" + eventCode + ">Confirmar transferencia</a></td>");92 } else if (action === "claim") {93 $("#table-own-promises tbody tr:last").append("<td><a href=promise/" + code + "><div class='description dotdotdot'>" + description + "</div></a>"94 + "<div class='date-action'>" + lastDateAction + "</div><div class='text-action'>" + actionText + " ti</div><a href=promise/" + code + ">Comentar moneda</a></td>");95 } else {96 $("#table-own-promises tbody tr:last").append("<td ><a href=promise/" + code + "><div class='description dotdotdot'>" + description + "</div></a>" +97 "<div class='date-action'>" + lastDateAction + "</div><div class='text-action'>" + actionText + " ti</div></td>");98 }99 $("#table-own-promises tbody td:last").append("<div class='responsive-actions'><div class='date-action'>"+datePromise+"</div><div class='text-action'>Moneda creada por " + responsible + "</div><a href='profile?info=" + responsibleCode + "' > <div> <b>==> Ver perfil público</b> </div></a></div>");100 $("#table-own-promises tbody tr:last").append("<td class='actions'><div class='date-action'>"+datePromise+"</div><div class='text-action'>Moneda creada por " + responsible + "</div><a href='profile?info=" + responsibleCode + "' > <div> <b>==> Ver perfil público</b> </div></a></td>");101 }102 $("#table-own-promises .dotdotdot").dotdotdot({103 ellipsis: '... ',104 wrap: 'word',105 height: 100,106 watch: true107 });108 if ($("#table-own-promises tbody tr").length > 0) {109 // $("body").animate({scrollTop: $("#table-own-promises tbody .scroll-mark:last").offset().top - 200}, 2000);110 }111 if (json.nextOwnPromises === "more") {112 $("#count-own-promises").text("(" + $("#table-own-promises tbody tr").length + "+)");113 $("#btn-more-own-promises").show("slow");114 } else {115 $("#count-own-promises").text("(" + $("#table-own-promises tbody tr").length + ")");116 }117 }118 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var util = require('util');3var fs = require('fs');4var test = wpt('www.webpagetest.org');5var testoptions = {6};7test.runTest(testurl, testoptions, function (err, data) {8 console.log('Test ID: ' + data.data.testId);9 console.log('View the test at: ' + data.data.summary);10 console.log('View the test results at: ' + data.data.userUrl);11 console.log('View the test video at: ' + data.data.videoUrl);12 console.log('View the test waterfall at: ' + data.data.waterfallUrl);13 console.log('View the test har at: ' + data.data.harUrl);14 console.log('View the test png at: ' + data.data.pngUrl);15 console.log('View the test png screen shot at: ' + data.data.screenShotUrl);16 console.log('View the test json at: ' + data.data.jsonUrl);17 console.log('View the test xml at: ' + data.data.xmlUrl);18 console.log('View the test pagespeed at: ' + data.data.pagespeedUrl);19 console.log('View the test pagespeed optimized at: ' + data.data.optimizedUrl);20 console.log('View the test pagespeed optimized png at: ' + data.data.optimizedPngUrl);21 console.log('View the test pagespeed optimized png screen shot at: ' + data.data.optimizedScreenShotUrl);22 console.log('View the test pagespeed optimized json at: ' + data.data.optimizedJsonUrl);23 console.log('View the test pagespeed optimized xml at: ' + data.data.optimizedXmlUrl);24 console.log('View the test pagespeed optimized har at: ' + data.data.optimizedHarUrl);25 console.log('View the test pagespeed optimized waterfall at: ' + data.data.optimizedWaterfallUrl);26 console.log('View the test pagespeed optimized video at: ' + data.data.optimizedVideoUrl);27 console.log('View the test pagespeed optimized video screen shot at

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org','A.12345678901234567890123456789012');3var testPromise = function() {4 var testPromise = wpt.runTest(url);5 testPromise.then(function(data) {6 console.log('testPromise: ', data);7 }, function(err) {8 console.log('testPromise error: ', err);9 });10};11testPromise();

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var runTest = function() {4 console.log('runTest');5 return new Promise(function(resolve, reject) {6 wpt.runTest(testUrl, {7 }, function(err, data) {8 if (err) {9 reject(err);10 } else {11 resolve(data);12 }13 });14 });15};16var getResults = function(data) {17 console.log('getResults');18 return new Promise(function(resolve, reject) {19 wpt.getTestResults(data.data.testId, function(err, data) {20 if (err) {21 reject(err);22 } else {23 resolve(data);24 }25 });26 });27};28runTest()29 .then(getResults)30 .then(function(data) {31 console.log(data);32 })33 .catch(function(err) {34 console.log(err);35 });36{ data:37 { testId: '160724_6N_1C',38 statusText: 'Ok' }39var wpt = require('webpagetest');40var wpt = new WebPageTest('www.webpagetest.org');41var runTest = function() {42 console.log('runTest');43 return new Promise(function(resolve,

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest'),2 wpt = new WebPageTest('www.webpagetest.org', 'A.4e8d0b7ac9c0f9e7d3f3b3d7d6c78e0f');3var test = function (url, options, callback) {4 wpt.runTest(url, options, function (err, data) {5 if (err) return console.log(err);6 console.log("Test submitted: " + data.data.testId);7 callback(data.data.testId);8 });9}10var result = function (testId, callback) {11 wpt.getTestResults(testId, function (err, data) {12 if (err) return console.log(err);13 callback(data);14 });15}16 console.log("Test Submitted");17 result(testId, function (data) {18 console.log("Test Complete");19 console.log(data);20 });21});22var WebPageTest = require('webpagetest');23var wpt = new WebPageTest('www.webpagetest.org', 'A.4e8d0b7ac9c0f9e7d3f3b3d7d6c78e0f');24var test = function (url, options) {25 return new Promise(function (resolve, reject) {26 wpt.runTest(url, options, function (err, data) {27 if (err) return reject(err);28 console.log("Test submitted: " + data.data.testId);29 resolve(data.data.testId);30 });31 });32}33var result = function (testId) {34 return new Promise(function (resolve, reject) {35 wpt.getTestResults(testId, function (err, data) {36 if (err) return reject(err);37 resolve(data);38 });39 });40}41 .then(function (testId) {42 console.log("Test Submitted");43 return result(testId);44 })45 .then(function (data) {46 console.log("Test Complete");47 console.log(data

Full Screen

Using AI Code Generation

copy

Full Screen

1const wpt = require('webpagetest');2const test = wpt('www.webpagetest.org', 'A.9f9c4d7b8a4c0f7f0e4d4d7e4c4f4e4');3}, function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10const wpt = require('webpagetest');11const test = wpt('www.webpagetest.org', 'A.9f9c4d7b8a4c0f7f0e4d4d7e4c4f4e4');12}).then((data) => {13 console.log(data);14}).catch((err) => {15 console.log(err);16});17 if (err) {18 at Object.<anonymous> (test.js:9:8)19 at Module._compile (module.js:653:30)20 at Object.Module._extensions..js (module.js:664:10)21 at Module.load (module.js:566:32)22 at tryModuleLoad (module.js:506:12)23 at Function.Module._load (module.js:498:3)24 at Function.Module.runMain (module.js:694:10)25 at startup (bootstrap_node.js:204:16)

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful