• Nie Znaleziono Wyników

JavaScript promises

N/A
N/A
Protected

Academic year: 2021

Share "JavaScript promises"

Copied!
9
0
0

Pełen tekst

(1)

JavaScript promises

Michał Okulewicz

Warsaw University of Technology

(2)

Asynchronous programming

• General idea:

– The code is executed in order defined by available resources and available data

– The task of the developer is to define the data necessary to compute a given value – One may think about it as an event-

driven programming

(3)

Asynchronous programming

A B C D E

Res(A,B,C) Res(D,E)

Res(Res(A,B,C),Res(D,E))

Developer defines the relation between

data and results

The final result

is computed

(4)

JavaScript Promise object

• After developer.mozilla.org:

– The Promise object is used for asynchronous computations

– A Promise represents a value which

may be available now, or in the future, or

never

(5)

Promise example

var promise = new Promise(function (resolve, reject) { var result = Math.random();

setTimeout(function () { if (result < 0.8) { resolve(result);

} else {

reject("No luck!");

}

}, 1000);

});

promise

.then(function (val) {

console.log('Promise fulfilled with ' + val);

})

(6)

Promises in jQuery

• Promises are wrapped up in a jQuery Deferred object

• jQuery nicely supports waiting for multiple

promises with a jQuery when() function

(7)

Promise example in jQuery

promise = $.Deferred();

var result = Math.random();

setTimeout(function () { if (result < 0.8) {

promise.resolve(result);

} else {

promise.reject("No luck!");

}

}, 1000);

promise

.then(function (val) {

console.log('Promise fulfilled with ' + val);

})

.catch(function (msg) {

(8)

Multiple promises

$().ready(function () {

var val1 = $.Deferred();

var val2 = $.Deferred();

$.when(val1, val2) .then(printResult) $.when(val1)

.then(console.log);

$.when(val2)

.then(console.log);

$('#val1').change(function () { val1.resolve(this.value);

});

$('#val2').change(function () { val2.resolve(this.value);

});

});

function printResult(a1, a2) {

$('#result').html(Number(a1) + Number(a2));

}

(9)

Animation

$().ready(function () {

$('#square').click(function () { var anim = $(this).animate(

{ left: '+=10px', top: '+=5px' }, 1000);

$

.when(anim)

.then(function () {

console.log('Animation finished (' + $(this).position().top + ',' + $(this).position().left + ')');

});

});

});

Cytaty

Powiązane dokumenty

Ex- plosive mixtures of dust and air may form during transport (e.g. in bucket elevators) and during the storage of raw mate- rials such as cereals, sugar and flour. An explosion

obszary, wydaje się jednak, iż to właśnie w dziedzinie kultury, a w szczególności teatru, omówione zjawiska miały charakter bardziej trwały, co widać w powrocie ciała w ruchu

Brak takich dociekań, jeżeli za ich przedmiot przyjąć potoczną świadomość w zakresie zdrowia i choroby oraz wynikające stąd dyrektywy zachowań, wydaje się być bardzo

For the load case L10, the stress range decreases by 55.5%, as shown in Figure 16b, which indicates that the optimization of diaphragm cutouts can effectively reduce the stress

In such perspective the future scenario of automotive propulsion systems will be achieved, in parallel, by further en- hancement of “conventional” reciprocating engines, by “best”

W stosunku więc do takich członków zarządu, którzy jednocześnie peł­ nią funkcję kierownika zakładu bądź zastępcy kierownika, będą miały za­ stosowanie

Oblasti takového výzkumu jsou potom: toponymie různých sociálních skupin, sociální aspekty toponymické kompetence (znalosti a užívání toponym), ne-