如何从具有多个ajax请求的函数中返回正确的值?

huangapple go评论72阅读模式
英文:

How can I return correct value from function with multiple ajax requests?

问题

我有一个函数,它包含两个嵌套的Ajax调用。我想在第二个Ajax调用成功时从中返回一个值。我认为这是因为函数已经结束,但Ajax调用仍未结束。但我没有解决它。

我准备了下面的示例代码:

var patato = function(){
    $.ajax({
        type: 'POST',
        url: 'https://jsonplaceholder.typicode.com/posts',
        dataType: 'json',
        data: {
            title: 'foo',
            body: 'bar',
            userId: 1,
        },
        success:function(res){
            console.log("Work 1")
            $.ajax({
                type: 'POST',
                url: 'https://jsonplaceholder.typicode.com/posts',
                dataType: 'json',
                data: {
                    title: 'foo',
                    body: 'bar',
                    userId: 1,
                },
                success:function(res){
                    console.log("Work 2")
                    return true;
                }
            })
        }
    })
    console.log("Çalıştı 3")
}
var patatos = patato();
if(patatos) {
    console.log("Patato true")
}else{
    console.log("Patato false")
}

请注意,这个代码示例中的问题是,patato 函数不会返回任何值,因为Ajax调用是异步的,函数会在Ajax调用完成之前结束。这就是为什么无法直接从第二个Ajax调用中返回值的原因。要解决这个问题,您可以使用回调函数或者Promise来处理Ajax异步调用的结果。

英文:

I have a function and its have nested two ajax call. I want to return a value inside from second ajax call when ajax call is successed. i think because function ending but the ajaxs call still not end. But i didn't solve it.

I prepared a example code below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    var patato = function(){
			$.ajax({
				type: &#39;POST&#39;,
				url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
				dataType: &#39;json&#39;,
				data: {
					title: &#39;foo&#39;,
					body: &#39;bar&#39;,
					userId: 1,
				},
				success:function(res){
					console.log(&quot;Work 1&quot;)
					$.ajax({
						type: &#39;POST&#39;,
						url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
						dataType: &#39;json&#39;,
						data: {
							title: &#39;foo&#39;,
							body: &#39;bar&#39;,
							userId: 1,
						},
						success:function(res){
							console.log(&quot;Work 2&quot;)
							return true;
						}
					})
				}
			})
			console.log(&quot;&#199;alıştı 3&quot;)
		}
		var patatos = patato();
		if(patatos) {
			console.log(&quot;Patato true&quot;)
		}else{
			console.log(&quot;Patato false&quot;)
		}

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-3.7.0.min.js&quot; integrity=&quot;sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 3

Your function should return a promise since you are doing some async work (fetching).

Also, I strongly recommend using async/await to simplify logic and improve readability/maintainability:

async function patato() {

    console.log("Çalıştı 3")

    const res = await $.ajax({
        type: 'POST',
        url: 'https://jsonplaceholder.typicode.com/posts',
        dataType: 'json',
        data: {
            title: 'foo',
            body: 'bar',
            userId: 1,
        }
    });

    console.log("Work 1")

    // use res if needed...

    const res2 = await $.ajax({
        type: 'POST',
        url: 'https://jsonplaceholder.typicode.com/posts',
        dataType: 'json',
        data: {
            title: 'foo',
            body: 'bar',
            userId: 1,
        },
    });

    console.log("Work 2")

    // use res2 if needed...

    return true;

}

patato().then(patato => console.log(`Patato ${patato}`));
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
英文:

Your function should return a promise since you are doing some async work (fetching).

Also I strongly recommend to use async/await to simplify logic and improve readability/maintainability:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

async function patato() {

    console.log(&quot;&#199;alıştı 3&quot;)

    const res = await $.ajax({
        type: &#39;POST&#39;,
        url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
        dataType: &#39;json&#39;,
        data: {
            title: &#39;foo&#39;,
            body: &#39;bar&#39;,
            userId: 1,
        }
    });

    console.log(&quot;Work 1&quot;)

    //use res if needed...

    const res2 = await $.ajax({
        type: &#39;POST&#39;,
        url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
        dataType: &#39;json&#39;,
        data: {
            title: &#39;foo&#39;,
            body: &#39;bar&#39;,
            userId: 1,
        },
    });

    console.log(&quot;Work 2&quot;)

    //use res2 if needed...

    return true;

}

patato().then(patato =&gt; console.log(`Patato ${patato}`));

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-3.7.0.min.js&quot; integrity=&quot;sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 2

以下是您要翻译的部分:

"You can also run both AJAX jobs in parallel if the output of the first job is not required for the second one:

async function patato() {
    console.log('Çalıştı 3');
    await $.when($.ajax({ type: 'POST', url: 'https://jsonplaceholder.typicode.com/posts', dataType: 'json', data: { title: 'foo1', body: 'bar1', userId: 1 } }),
        $.ajax({ type: 'POST', url: 'https://jsonplaceholder.typicode.com/posts', dataType: 'json', data: { title: 'foo2', body: 'bar2', userId: 2 } })
    ).done(function ([r1], [r2]) {
        console.log('Both AJAX jobs are finished.', r1, r2);
    });
    console.log('End of patato.');
    return true;
}

patato().then(pa => console.log(`Patato returns ${pa}`));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
```"

<details>
<summary>英文:</summary>

You can also run both AJAX jobs in parallel if the output of the first job is not required for the second one:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    async function patato() {

        console.log(&quot;&#199;alıştı 3&quot;)
        await $.when( $.ajax({type:&#39;POST&#39;,url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
                             dataType:&#39;json&#39;,data: {title: &#39;foo1&#39;,body: &#39;bar1&#39;,userId: 1}}),
                      $.ajax({type: &#39;POST&#39;,url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
                             dataType:&#39;json&#39;,data: {title: &#39;foo2&#39;,body: &#39;bar2&#39;,userId: 2}})
        ).done(function([r1],[r2]){console.log(&quot;Both AJAX jobs are finished.&quot;,r1,r2)})
        console.log(&quot;End of patato.&quot;);
        return true;
    }

    patato().then(pa =&gt; console.log(`Patato returns ${pa}`));

&lt;!-- language: lang-html --&gt;

    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;


&lt;!-- end snippet --&gt;



</details>



# 答案3
**得分**: 1

以下是您要翻译的部分:

"First thing that I wanted to mention is that it will not make much sense to make sequential requests if you second request don't need the response from the first one. On that note, I will assume you have a use case that requires two API call executed one after the other.

Here you have two options: use callbacks or `async/await` syntax.
Here you can find a good example of the usage of both: [Stack Overflow 链接](https://stackoverflow.com/questions/48506445/how-to-return-an-ajax-result-using-async-await)

For your particular example here's how you could do this with callbacks:

```js
const patato = function(){
    return $.ajax({
        type: 'POST',
        url: 'https://jsonplaceholder.typicode.com/posts',
        dataType: 'json',
        data: {
            title: 'foo',
            body: 'bar',
            userId: 1,
        }
    }).then(function(res1){ 
        console.log("Work 1")
        return $.ajax({
            type: 'POST',
            url: 'https://jsonplaceholder.typicode.com/posts',
            dataType: 'json',
            data: {
                title: 'foo',
                body: 'bar',
                userId: 1,
            }
        }).then(function(res2){
            console.log("Work 2")
            return res1 && res2;
        })
    })
}
patato().then(patatos => {
  if(patatos) {
      console.log(patatos)
  }else {
      console.log("Patato false")
  }
})

或者您也可以使用 async/await 语法(个人认为这比回调更容易理解):

const patato = async function(){
    console.log("Work 1");
    const res1 = await $.ajax({
        type: 'POST',
        url: 'https://jsonplaceholder.typicode.com/posts',
        dataType: 'json',
        data: {
            title: 'foo',
            body: 'bar',
            userId: 1,
        }
    });

    console.log("Work 2");
    const res2 = await $.ajax({
        type: 'POST',
        url: 'https://jsonplaceholder.typicode.com/posts',
        dataType: 'json',
        data: {
            title: 'foo',
            body: 'bar',
            userId: 1,
        }
    });
    return res1 && res2;
}

async function test() {
    let result = await patato();
    console.log(result);
}
test();

希望对您有帮助。

英文:

First thing that I wanted to mention is that it will not make much sense to make sequential requests if you second request don't need the response from the first one. On that note, I will assume you have a use case that requires two API call executed one after the other.

Here you have two options: use callbacks or async/await syntax.
Here you can find a good example of the usage of both: https://stackoverflow.com/questions/48506445/how-to-return-an-ajax-result-using-async-await

For your particular example here's how you could do this with callbacks:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

    const patato = function(){
            return $.ajax({
                type: &#39;POST&#39;,
                url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
                dataType: &#39;json&#39;,
                data: {
                    title: &#39;foo&#39;,
                    body: &#39;bar&#39;,
                    userId: 1,
                }
            }).then(function(res1){ 
                console.log(&quot;Work 1&quot;)
                    return $.ajax({
                        type: &#39;POST&#39;,
                        url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
                        dataType: &#39;json&#39;,
                        data: {
                            title: &#39;foo&#39;,
                            body: &#39;bar&#39;,
                            userId: 1,
                        }
                    }).then(function(res2){
                        console.log(&quot;Work 2&quot;)
                        return res1 &amp;&amp; res2;
                    })
            })
        }
        patato().then(patatos =&gt; {
          if(patatos) {
              console.log(patatos)
          }else {
              console.log(&quot;Patato false&quot;)
          }
        })

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-3.7.0.min.js&quot; integrity=&quot;sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

Or you could use async/await syntax as well (IMO this is easier to follow than the callback one):

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

    const patato = async function(){
            console.log(&quot;Work 1&quot;);
            const res1 = await $.ajax({
                type: &#39;POST&#39;,
                url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
                dataType: &#39;json&#39;,
                data: {
                    title: &#39;foo&#39;,
                    body: &#39;bar&#39;,
                    userId: 1,
                }
            });

            console.log(&quot;Work 2&quot;);
            const res2 = await $.ajax({
                type: &#39;POST&#39;,
                url: &#39;https://jsonplaceholder.typicode.com/posts&#39;,
                dataType: &#39;json&#39;,
                data: {
                    title: &#39;foo&#39;,
                    body: &#39;bar&#39;,
                    userId: 1,
                }
            });
            return res1 &amp;&amp; res2;

        }
        async function test() {
            let result = await patato();
            console.log(result);
        }
        test();

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-3.7.0.min.js&quot; integrity=&quot;sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 01:36:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定