mirror of
https://github.com/xfarrow/javascript-tutorials.git
synced 2025-04-16 19:37:20 +02:00
intentation
This commit is contained in:
parent
0e85666f23
commit
fa46b1fce5
@ -1,27 +1,26 @@
|
||||
// https://javascript.info/callbacks
|
||||
|
||||
entryPoint();
|
||||
entryPoint()
|
||||
|
||||
function entryPoint () {
|
||||
|
||||
execute_action('something', function (error, time_of_completion) {
|
||||
if (error) {
|
||||
console.log('Something happened');
|
||||
console.log('Something happened')
|
||||
} else {
|
||||
console.log('Time of completion: ' + new Date(time_of_completion).toDateString());
|
||||
console.log('Time of completion: ' + new Date(time_of_completion).toDateString())
|
||||
}
|
||||
});
|
||||
console.log("I don't need execute_action's value");
|
||||
})
|
||||
console.log("I don't need execute_action's value")
|
||||
}
|
||||
|
||||
function execute_action (param, callback) {
|
||||
if (param == 'something') {
|
||||
console.log('Executing action: ' + param);
|
||||
callback(null, Date.now());
|
||||
console.log('Executing action: ' + param)
|
||||
callback(null, Date.now())
|
||||
} else {
|
||||
// We can call callback with one argument even if
|
||||
// the signature states two parameters.
|
||||
callback(new Error('Invalid parameter'));
|
||||
callback(new Error('Invalid parameter'))
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,4 +53,3 @@ function entryPoint () {
|
||||
constructs.
|
||||
|
||||
*/
|
||||
|
@ -18,8 +18,8 @@
|
||||
callback function, but with many additional features and a more readable syntax.
|
||||
*/
|
||||
const promise = new Promise(function (resolve, reject) {
|
||||
setTimeout(() => resolve('done'), 5000);
|
||||
});
|
||||
setTimeout(() => resolve('done'), 5000)
|
||||
})
|
||||
|
||||
/*
|
||||
The first argument of .then is a function that runs when the promise is resolved and receives the result.
|
||||
@ -29,14 +29,14 @@ const promise = new Promise(function (resolve, reject) {
|
||||
promise.then(
|
||||
result => console.log('The operation was successful. It returned ' + result),
|
||||
error => console.log('The operation was not successful: ' + error)
|
||||
);
|
||||
)
|
||||
|
||||
/*
|
||||
Or we can pass only one argument if we're interested only in a positive result
|
||||
*/
|
||||
promise.then(
|
||||
result => console.log('The operation was successful. It returned ' + result)
|
||||
);
|
||||
)
|
||||
|
||||
/*
|
||||
Or we can pass only one argument to the method "catch" if we're interested
|
||||
@ -46,17 +46,17 @@ promise.then(
|
||||
*/
|
||||
promise.catch(
|
||||
error => console.log(error)
|
||||
);
|
||||
)
|
||||
|
||||
/*
|
||||
finally gets always called
|
||||
*/
|
||||
promise.finally(
|
||||
() => console.log('The execution has terminated. Bye')
|
||||
);
|
||||
)
|
||||
|
||||
/*
|
||||
This line is used to demonstrate that the code within "then, catch, etc."
|
||||
is in the event loop, as this is the first line getting executed.
|
||||
*/
|
||||
console.log("Last line");
|
||||
console.log('Last line')
|
||||
|
@ -4,17 +4,17 @@
|
||||
// internally calling resolve().
|
||||
|
||||
new Promise(function (resolve, reject) {
|
||||
setTimeout(() => resolve(1), 1);
|
||||
setTimeout(() => resolve(1), 1)
|
||||
}).then(function (result) {
|
||||
console.log(result);
|
||||
return result * 2;
|
||||
console.log(result)
|
||||
return result * 2
|
||||
}).then(function (result) {
|
||||
console.log(result);
|
||||
return result * 2;
|
||||
console.log(result)
|
||||
return result * 2
|
||||
}).then(function (result) {
|
||||
console.log(result);
|
||||
return result * 2;
|
||||
});
|
||||
console.log(result)
|
||||
return result * 2
|
||||
})
|
||||
|
||||
/*
|
||||
It will print
|
||||
@ -40,9 +40,9 @@ new Promise(function (resolve, reject) {
|
||||
.then(function (response) {
|
||||
// response.text() returns a new promise that resolves with the full response text
|
||||
// when it loads
|
||||
return response.text();
|
||||
return response.text()
|
||||
})
|
||||
.then(function (text) {
|
||||
// ...and here's the content of the remote file
|
||||
console.log(text);
|
||||
});
|
||||
console.log(text)
|
||||
})
|
||||
|
@ -4,10 +4,10 @@
|
||||
// Async and await are merely syntactic sugar in order to make Promise usage easier
|
||||
|
||||
async function f1 () {
|
||||
return 1;
|
||||
return 1
|
||||
}
|
||||
|
||||
f1().then(console.log); // 1
|
||||
f1().then(console.log) // 1
|
||||
|
||||
// The keyword await makes JavaScript wait until that promise settles and returns its result.
|
||||
// It can be used in async functions only.
|
||||
@ -15,12 +15,12 @@ f1().then(console.log); // 1
|
||||
// and then resumes it with the promise result.
|
||||
async function f2 () {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
setTimeout(() => resolve('done!'), 1000);
|
||||
});
|
||||
const result = await promise; // wait until the promise resolves (*)
|
||||
console.log(result); // "done!"
|
||||
setTimeout(() => resolve('done!'), 1000)
|
||||
})
|
||||
const result = await promise // wait until the promise resolves (*)
|
||||
console.log(result) // "done!"
|
||||
}
|
||||
f2();
|
||||
f2()
|
||||
|
||||
// The code in the same function after "await"
|
||||
// is to be intended in the "then()" of the primise. This means
|
||||
@ -28,15 +28,15 @@ f2();
|
||||
// the flow of execution goes out that code block. For example
|
||||
// consider the following example:
|
||||
async function exampleAsyncFunction () {
|
||||
console.log('Before await');
|
||||
console.log('Before await')
|
||||
await new Promise(function (resolve, reject) {
|
||||
setTimeout(() => resolve('done'), 500);
|
||||
}); // Pauses execution here until the promise resolves.
|
||||
console.log('After await');
|
||||
setTimeout(() => resolve('done'), 500)
|
||||
}) // Pauses execution here until the promise resolves.
|
||||
console.log('After await')
|
||||
}
|
||||
console.log('Start');
|
||||
exampleAsyncFunction();
|
||||
console.log('End');
|
||||
console.log('Start')
|
||||
exampleAsyncFunction()
|
||||
console.log('End')
|
||||
|
||||
// The result will be:
|
||||
// Start, Before Await, End, After await
|
||||
|
Loading…
x
Reference in New Issue
Block a user