0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScriptめも

Last updated at Posted at 2022-02-05
sample.js

//関数宣言
function add(x, y) {
  return x + y;
};
console.log(add(1, 2));  // => 3

//即時関数
(function (x, y) {
    console.log(x + y);
}(1, 2)); // => 3

//関数代入
var test = function (x, y) {
    return x + y;
};
console.log(test(1, 2)); // => 3

//ラムダ
var test = (x, y) => x + y;
console.log(test(1,2)); // => 3

//配列
var array = [1,2,3]
console.log(array[0]) //=> 1

//連想配列
var array2 = {
            a : true,
            b  : false,
            c : false
        };
console.log(array2['a']) //=> true

//Promise then 非同期処理
var sample = new Promise(function(resolve, reject) {
    setTimeout(function() {
        resolve();
    }, 1000);
});
sample.then(function(value) {
    console.log("Promise成功!");
});
console.log("先に出力");

非同期

asyncとawait

sample.js

const test = async () => {
    console.log(1);
    await new Promise((resolve) => {
        setTimeout(() => {
            console.log(2);
            resolve();
        },1000)
    })
}

const main = async () => {
    await test();
    console.log(3);
}

main()

testからasyncとawaitを外す

sample.js
const test = () => {
    console.log(1);
     return new Promise((resolve) => {
        setTimeout(() => {
            console.log(2);
            resolve();
        },1000)
    })
}

const main = async () => {
    await test();
    console.log(3);
}

main()

mainからもasyncとawaitを外す

sample.js
const test = () => {
    console.log(1);
     return new Promise((resolve) => {
        setTimeout(() => {
            console.log(2);
            resolve();
        },1000)
    })
}

const main = async () => {
    await test();
    console.log(3);
}

main()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?