At the end of this class, you will be able to:
wilson-espina/jsd-9-resources
repo to your computerDocuments/JSD/jsd-9-resources/11-async-callbacks
let status
function doSomething() {
for (let i = 0; i < 1000000000; i++) {
numberArray.push(i)
}
status = 'done'
console.log('First function done')
}
function doAnotherThing() {
console.log('Second function done')
}
function doSomethingElse() {
console.log('Third function: '+ status)
}
doSomething()
doAnotherThing()
doSomethingElse()
// result in console
// (after a few seconds):
> "First function done"
> "Second function done"
> "Third function: done"
$('button').on('click', doSomething);
$.ajax(url, function(data) {
doAnotherThing(data)
});
fetch(url).then(response => {
if (response.ok) {
return response.json();
} else {
console.log('There was a problem.');
}
}).then(doSomethingElse);
$('button').on('click', function() {
// your code here
});
$('button').on(argument1, argument2);
const getWeather = (weatherUrl, city, apiKey) => {
$.ajax({
url: weatherUrl + city + '&appid=' + apiKey,
success: function (response) {
updateUISuccess(response.main.temp)
},
error: function () {
updateUIError();
},
});
};
const programmeModule = (() => {
let programmes = [];
return {
addProgramme: function(newProgramme) {
programmes.push(newProgramme);
},
getProgrammes: function() {
return programmes
}
}
})()
function helloWorld() {
console.log("Hello world");
}
setTimeout(helloWorld, 1000);
setTimeout()
setTimeout(function, delay);
function
is a function(reference or anonymous.delay
is a time in milliseconds to wait before the first argument is called.
setTimeout(() => {
console.log("Hello world")
}, 1000);
setTimeout
with anonymous function argument
function helloWorld() {
console.log("Hello world");
}
setTimeout(helloWorld, 1000);
setTimeout
with named function argument
setTimeout()
or forEach()
) or it can be a reference to a function defined elsewhere.Open up: 00-callback-codealong
Objective
Location
starter-code > 01-callback-exercise
Timing
20 mins |
|
doSomething((result) => {
doSomethingElse(result, (newResult) => {
doThirdThing(newResult, (finalResult) => {
console.log('Got the final result: ' + finalResult);
}, failureCallback);
}, failureCallback);
}, failureCallback);
Traditional Callback:
doSomething(successCallback, failureCallback);
Callback using a promise:
doSomething()
.then(successCallback) // work with result
.catch(failureCallback) // handle error
doSomething()
.then(result1 => doSomethingElse(result1))
.then(result2 => doThirdThing(result2))
.then(result3 => {
console.log('Got the final result: ' + result3)
})
.catch((error) => {
console.log('There was an error: ' + error);
});
const fooPromise = () => {
return new Promise(function(resolve, reject) {
});
}
const fooPromise = () => {
return new Promise(function(resolve, reject) {
if (/* everything turned out fine */) {
resolve("Stuff worked!");
} else {
reject(Error("It broke"));
}
});
}
const fooPromise = () => {
return new Promise(function(resolve, reject) {
setTimeout(() => {
resolve('foo')
}, 5000)
});
}
fooPromise()
.then(value => {
console.log(value) /* expected output: "foo" */
});
At any stage, a Promise will be in one of these 4 states:
fetch(url)
.then(function (response) {
if (response.ok) {
return response.json();
} else {
throw 'Network response was not ok.';
}
}).then(function (data) {
// DOM manipulation
}).catch(function (error) {
// handle lack of data in UI
});
Open up: 02-fetch-codealong
Location
starter-code > 03-async-lab
Timing
20 mins |
|
(Lesson #11)