質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
同期

複数のディレクトリに存在するファイルを更新した場合に、すべてのファイルにも更新が行われる事、又は、同じ記憶領域に同時にアクセスして内容の整合性が失われてしまう事をを防ぐ制御などを同期と呼びます。

非同期処理

非同期処理とは一部のコードを別々のスレッドで実行させる手法です。アプリケーションのパフォーマンスを向上させる目的でこの手法を用います。

Q&A

解決済

1回答

8108閲覧

axiosを同期処理したい

退会済みユーザー

退会済みユーザー

総合スコア0

同期

複数のディレクトリに存在するファイルを更新した場合に、すべてのファイルにも更新が行われる事、又は、同じ記憶領域に同時にアクセスして内容の整合性が失われてしまう事をを防ぐ制御などを同期と呼びます。

非同期処理

非同期処理とは一部のコードを別々のスレッドで実行させる手法です。アプリケーションのパフォーマンスを向上させる目的でこの手法を用います。

0グッド

0クリップ

投稿2021/10/16 14:34

###やりたいこと
下に記載した[結果]を1,2,3と表示したい

前提

  • axiosとPDOでデータベースからデータを引っ張ってきたい
  • 複数の関数やVueインスタンスで同じデータを扱いたいので、グローバル変数に入れるようにしたい
  • データを更新できるように、関数として宣言したい
  • promise形式で返されても困るので、返り値はオブジェクト型にしたい

js

1data_get = function() { 2 return axios.get('../../php/select.php') 3 .then(function (response) { 4 return response.data; 5 }).catch(function (error) { 6 console.log(error); 7 }) 8} 9var aaa = "aaa"; 10data_get().then(function (x) { 11 console.log(1); 12 console.log(x); 13 aaa = x; 14 console.log(2); 15 console.log(aaa); 16}); 17console.log(3); 18console.log(aaa);

結果

chromeDeveropertoolConsole

13 2aaa 3 41 5[(14){…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 6 72 8[(14){…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

やったこと

関数をasyncにした
結果:かわらず

js

1data_get = function() { 2 return axios.get('../../php/select.php') 3 .then(function (response) { 4 return response.data; 5 }).catch(function (error) { 6 console.log(error); 7 }) 8} 9var aaa = "aaa"; 10data_get().then(function (x) { 11 console.log(1); 12 console.log(x); 13 aaa = x; 14 console.log(2); 15 console.log(aaa); 16}); 17console.log(3); 18console.log(aaa);

結果

chromeDeveropertoolConsole

13 2aaa 3 41 5[(14){…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 6 72 8[(14){…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 9```--- 10 11関数をasyncにして、returnにawaitを付けた 12結果:かわらず 13```js 14data_get = function() { 15 return await axios.get('../../php/select.php') 16 .then(function (response) { 17 return response.data; 18 }).catch(function (error) { 19 console.log(error); 20 }) 21} 22var aaa = "aaa"; 23data_get().then(function (x) { 24 console.log(1); 25 console.log(x); 26 aaa = x; 27 console.log(2); 28 console.log(aaa); 29}); 30console.log(3); 31console.log(aaa);

結果

chromeDeveropertoolConsole

13 2aaa 3 41 5[(14){…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 6 72 8[(14){…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

関数をasyncにして、実行時にawaitを付けた
結果:エラーが出た

js

1data_get = async function() { 2 return axios.get('../../php/select.php') 3 .then(function (response) { 4 return response.data; 5 }).catch(function (error) { 6 console.log(error); 7 }) 8} 9var aaa = "aaa"; 10await data_get().then(function (x) { 11 console.log(1); 12 console.log(x); 13 aaa = x; 14 console.log(2); 15 console.log(aaa); 16}); 17console.log(3); 18console.log(aaa);

結果

chromeDeveropertoolConsole

1Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

不可能です。async-awaitPromiseを返す関数の別表記に過ぎません。

投稿2021/10/16 22:49

maisumakun

総合スコア145201

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/10/17 04:21

非同期処理と非同期通信について、ちゃんと勉強しようと思います。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問