ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺎﻫ ﻩﺪﻋﻭ


فهرست مطالب

    نمایش فهرست مطالب

"ﻢﻫﺩ ﯽﻣ ﻝﻮﻗ ﺍﺭ ﻪﺠﯿﺘﻧ ﮏﯾ ﻦﻣ!"

ﺪﺸﮑﺑ ﻝﻮﻃ ﯽﻤﮐ ﺖﺳﺍ ﻦﮑﻤﻣ ﻪﮐ ﺖﺳﺍ ﯼﺪﮐ "ﺪﮐ ﺪﯿﻟﻮﺗ"

ﺪﺷﺎﺑ ﻪﺠﯿﺘﻧ ﺮﻈﺘﻨﻣ ﺪﯾﺎﺑ ﻪﮐ ﺖﺳﺍ ﯼﺪﮐ "ﻩﺪﻨﻨﮐ ﻑﺮﺼﻣ ﺪﮐ"

Promise ﺪﻫﺩ ﯽﻣ ﺪﻧﻮﯿﭘ ﻢﻫ ﻪﺑ ﺍﺭ ﻩﺪﻨﻨﮐ ﻑﺮﺼﻣ ﺪﮐ ﻭ ﺪﮐ ﺪﯿﻟﻮﺗ ﻪﮐ ﺖﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ

ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻩﺪﻋﻭ ءﯽﺷ

:ﺖﺳﺍ ﻩﺪﻨﻨﮐ ﻑﺮﺼﻣ ﺪﮐ ﯽﻧﺍﻮﺧﺍﺮﻓ ﻭ ﻩﺪﻨﻨﮐ ﺪﯿﻟﻮﺗ ﺪﮐ ﻞﻣﺎﺷ Promise ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ

ﻝﻮﻗ ﻮﺤﻧ

let myPromise = new Promise(function(myResolve, myReject) {
// "Producing Code" (May take some time)

  myResolve(); // when successful
  myReject();  // when error
});

// "Consuming Code" (Must wait for a fulfilled Promise)
myPromise.then(
  function(value) { /* code if successful */ },
  function(error) { /* code if some error */ }
);

:ﺪﻨﮐ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺍﺭ callback ﻭﺩ ﺯﺍ ﯽﮑﯾ ﺪﯾﺎﺑ ،ﺩﺭﻭﺁ ﺖﺳﺩ ﻪﺑ ﺍﺭ ﻪﺠﯿﺘﻧ ﻩﺪﻨﻨﮐ ﺪﯿﻟﻮﺗ ﺪﮐ ﯽﺘﻗﻭ

Success

myResolve (ﻪﺠﯿﺘﻧ ﺭﺍﺪﻘﻣ)

Error

myReject (ﺎﻄﺧ ءﯽﺷ)


Promise Object Properties

:ﺪﺷﺎﺑ ﺪﻧﺍﻮﺗ ﯽﻣ Promise ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ

  • ﺭﺎﻈﺘﻧﺍ

  • ﺪﺷ ﻩﺩﺭﻭﺁﺮﺑ

  • ﺪﺷ ﺩﺭ

.resultstate :ﺪﻨﮐ ﯽﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ ﯽﮔﮋﯾﻭ ﻭﺩ ﺯﺍ Promise ﯽﺷ

.ﺖﺳﺍ ﻩﺪﺸﻧ ﻒﯾﺮﻌﺗ ﻥﺁ ﻪﺠﯿﺘﻧ ،ﺖﺳﺍ "ﺭﺎﮐ ﻝﺎﺣ ﺭﺩ" Promise ﯽﺷ ﮏﯾ ﻪﮐ ﯽﻟﺎﺣ ﺭﺩ

.ﺖﺳﺍ ﺭﺍﺪﻘﻣ ﮏﯾ ﻪﺠﯿﺘﻧ ،"ﺪﺑﺎﯾ ﯽﻣ ﻖﻘﺤﺗ" Promise ﯽﺷ ﮏﯾ ﻪﮐ ﯽﻣﺎﮕﻨﻫ

.ﺖﺳﺍ ﺎﻄﺧ ﯽﺷ ﮏﯾ ﻪﺠﯿﺘﻧ ،"ﺪﺷ ﺩﺭ" Promise ﯽﺷ ﮏﯾ ﻪﮐ ﯽﻣﺎﮕﻨﻫ

"pending"

ﻩﺪﺸﻧ ﻒﯾﺮﻌﺗ

"fulfilled"

ﻪﺠﯿﺘﻧ ﺭﺍﺪﻘﻣ ﮏﯾ

"rejected"

ﺎﻄﺧ ﯽﺷ ﮏﯾ

.ﺪﯿﻨﮐ ﺍﺪﯿﭘ ﯽﺳﺮﺘﺳﺩ Promise ﺹﺍﻮﺧ ﻪﺠﯿﺘﻧﺖﯿﻌﺿﻭ ﻪﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ ﺎﻤﺷ

.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ Promise ﺵﻭﺭ ﺯﺍ ﺪﯾﺎﺑ ﺎﻫ ﻩﺪﻋﻭ ﻪﺑ ﯽﮔﺪﯿﺳﺭ ﯼﺍﺮﺑ


ﻪﻧﻮﮕﭼ ﻝﻮﻗ

:ﺖﺳﺍ ﻩﺪﻣﺁ Promise ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻩﻮﺤﻧ ﺎﺠﻨﯾﺍ ﺭﺩ

myPromise.then(
  function(value) { /* code if successful */ },
  function(error) { /* code if some error */ }
);

Promise.then() ﮏﯾ ،ﺩﺮﯿﮔ ﯽﻣ ﻥﺎﻣﻮﮔﺭﺁ ﻭﺩ callback ﺖﺴﮑﺷ ﯼﺍﺮﺑ ﯼﺮﮕﯾﺩ ﻭ ﺖﯿﻘﻓﻮﻣ ﯼﺍﺮﺑ.

.ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ ﯽﺘﺸﮔﺮﺑ ﺱﺎﻤﺗ ﮏﯾ ،ﺖﺴﮑﺷ ﺎﯾ ﺖﯿﻘﻓﻮﻣ ﯼﺍﺮﺑ ﻂﻘﻓ ﺪﯿﻧﺍﻮﺗﯽﻣ ﻦﯾﺍﺮﺑﺎﻨﺑ ،ﺪﻨﺘﺴﻫ ﯼﺭﺎ

ﻝﺎﺜﻣ

function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;
}

let myPromise = new Promise(function(myResolve, myReject) {
  let x = 0;

// The producing code (this may take some time)

  if (x == 0) {
    myResolve("OK");
  } else {
    myReject("Error");
  }
});

myPromise.then(
  function(value) {myDisplayer(value);},
  function(error) {myDisplayer(error);}
);

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Promise</h2>

<p id="demo"></p>

<script>
function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;
}

let myPromise = new Promise(function(myResolve, myReject) {
  let x = 0;

// some code (try to change x to 5)

  if (x == 0) {
    myResolve("OK");
  } else {
    myReject("Error");
  }
});

myPromise.then(
  function(value) {myDisplayer(value);},
  function(error) {myDisplayer(error);}
);
</script>

</body>
</html>


ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻩﺪﻋﻭ ﯼﺎﻫ ﻪﻧﻮﻤﻧ

:ﺩﺮﮐ ﻢﯿﻫﺍﻮﺧ ﻩﺩﺎﻔﺘﺳﺍ ﻞﺒﻗ ﻞﺼﻓ ﺱﺎﻤﺗ ﻪﺑ ﺖﺸﮔﺮﺑ ﯼﺎﻫﻝﺎﺜﻣ ﺯﺍ ،ﺎﻫﻩﺪﻋﻭ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻥﺩﺍ

  • ﯽﻧﺎﻣﺯ ﺖﻠﻬﻣ ﺭﺎﻈﺘﻧﺍ ﺭﺩ

  • ﻞﯾﺎﻓ ﮏﯾ ﺭﺎﻈﺘﻧﺍ ﺭﺩ


ﯽﻧﺎﻣﺯ ﺖﻠﻬﻣ ﺭﺎﻈﺘﻧﺍ ﺭﺩ

Callback ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻝﺎﺜﻣ

setTimeout(function() { myFunction("I love You !!!"); }, 3000);

function myFunction(value) {
  document.getElementById("demo").innerHTML = value;
}

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Functions</h1>
<h2>setInterval() with a Callback</h2>

<p>Wait 3 seconds (3000 milliseconds) for this page to change.</p>

<h1 id="demo"></h1>

<script>
setTimeout(function() { myFunction("I love You !!!"); }, 3000);

function myFunction(value) {
  document.getElementById("demo").innerHTML = value;
}
</script>

</body>
</html>

Promise ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻝﺎﺜﻣ

let myPromise = new Promise(function(myResolve, myReject) {
  setTimeout(function() { myResolve("I love You !!"); }, 3000);
});

myPromise.then(function(value) {
  document.getElementById("demo").innerHTML = value;
});

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Promise</h2>

<p>Wait 3 seconds (3000 milliseconds) for this page to change.</p>

<h1 id="demo"></h1>

<script>
const myPromise = new Promise(function(myResolve, myReject) {
  setTimeout(function(){ myResolve("I love You !!"); }, 3000);
});

myPromise.then(function(value) {
  document.getElementById("demo").innerHTML = value;
});
</script>

</body>
</html>

ﻞﯾﺎﻓ ﺮﻈﺘﻨﻣ

Callback ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻝﺎﺜﻣ

function getFile(myCallback) {
  let req = new XMLHttpRequest();
  req.open('GET', "mycar.html");
  req.onload = function() {
    if (req.status == 200) {
      myCallback(req.responseText);
    } else {
      myCallback("Error: " + req.status);
    }
  }
  req.send();
}

getFile(myDisplayer);

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Callbacks</h2>

<p id="demo"></p>

<script>
function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;
}

function getFile(myCallback) {
  let req = new XMLHttpRequest();
  req.onload = function() {
    if (req.status == 200) {
      myCallback(this.responseText);
    } else {
      myCallback("Error: " + req.status);
    }
  }
  req.open('GET', "mycar.html");
  req.send();
}

getFile(myDisplayer); 
</script>

</body>
</html>

Promise ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﻝﺎﺜﻣ

let myPromise = new Promise(function(myResolve, myReject) {
   
  let req = new XMLHttpRequest();
   
  req.open('GET', "mycar.htm");
   
  req.onload = function() {
       
    if (req.status == 200) {
           
      myResolve(req.response);
       
    } else {
           
      myReject("File not Found");
       
    }
  };
  req.send();
});

myPromise.then(
   
  function(value) {myDisplayer(value);},
   
  function(error) {myDisplayer(error);}
);

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Promise</h2>

<p id="demo"></p>

<script>
function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;
}

let myPromise = new Promise(function(myResolve, myReject) {
  let req = new XMLHttpRequest();
  req.open('GET', "mycar.html");
  req.onload = function() {
    if (req.status == 200) {
      myResolve(req.response);
    } else {
      myReject("File not Found");
    }
  };
  req.send();
});

myPromise.then(
  function(value) {myDisplayer(value);},
  function(error) {myDisplayer(error);}
);
</script>

</body>
</html>

ﺮﮔﺭﻭﺮﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ

ECMAScript 2015 ﻡﺎﻧ ﺎﺑ ﻪﮐ ES6 ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ،ﺩﻮﺷ ﯽﻣ ﻪﺘﺧﺎﻨﺷ ﺰﯿﻧ Promise ﺩﺮﮐ ﯽﻓﺮﻌﻣ ﺍﺭ

:ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ Promise ءﺎﯿﺷﺍ ﺯﺍ ﻞﻣﺎﮐ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺎﺑ ﺍﺭ ﺮﮔﺭﻭﺮﻣ ﻪﺨﺴﻧ ﻦﯿﻟﻭﺍ ﺮﯾﺯ ﻝﻭﺪﺟ

Chrome 33 Edge 12 Firefox 29 Safari 7.1 Opera 20
Feb, 2014 Jul, 2015 Apr, 2014 Sep, 2014 Mar, 2014

تمامی حقوق محفوظ است. © BasicIT.org • 2023-2024