control-freak-ide/Control-Freak-Documentation/jQuery/docs/entries/deferred.promise.json
plastic-hub-dev-node-saturn 538369cff7 latest
2021-05-12 18:35:18 +02:00

1 line
3.6 KiB
JSON

{"name":"deferred.promise","type":"method","title":"deferred.promise()","deprecated":null,"removed":null,"desc":" Return a Deferred's Promise object. ","categories":["deferred-object","version/1.5"],"entries":[{"return":"Promise","signatures":{"added":"1.5","argument":{"desc":"Object onto which the promise methods have to be attached","name":"target","type":"Object","optional":"true"}},"examples":[{"desc":"Create a Deferred and set two timer-based functions to either resolve or reject the Deferred after a random interval. Whichever one fires first \"wins\" and will call one of the callbacks. The second timeout has no effect since the Deferred is already complete (in a resolved or rejected state) from the first timeout action. Also set a timer-based progress notification function, and call a progress handler that adds \"working...\" to the document body.","code":"\nfunction asyncEvent() {\n var dfd = new jQuery.Deferred();\n\n // Resolve after a random interval\n setTimeout(function() {\n dfd.resolve( \"hurray\" );\n }, Math.floor( 400 + Math.random() * 2000 ) );\n\n // Reject after a random interval\n setTimeout(function() {\n dfd.reject( \"sorry\" );\n }, Math.floor( 400 + Math.random() * 2000 ) );\n\n // Show a \"working...\" message every half-second\n setTimeout(function working() {\n if ( dfd.state() === \"pending\" ) {\n dfd.notify( \"working... \" );\n setTimeout( working, 500 );\n }\n }, 1 );\n\n // Return the Promise so caller can't change the Deferred\n return dfd.promise();\n}\n\n// Attach a done, fail, and progress handler for the asyncEvent\n$.when( asyncEvent() ).then(\n function( status ) {\n alert( status + \", things are going well\" );\n },\n function( status ) {\n alert( status + \", you fail this time\" );\n },\n function( status ) {\n $( \"body\" ).append( status );\n }\n);\n"},{"desc":"Use the target argument to promote an existing object to a Promise:","code":"\n// Existing object\nvar obj = {\n hello: function( name ) {\n alert( \"Hello \" + name );\n }\n },\n // Create a Deferred\n defer = $.Deferred();\n\n// Set object as a promise\ndefer.promise( obj );\n\n// Resolve the deferred\ndefer.resolve( \"John\" );\n\n// Use the object as a Promise\nobj.done(function( name ) {\n obj.hello( name ); // Will alert \"Hello John\"\n}).hello( \"Karl\" ); // Will alert \"Hello Karl\"\n"}],"longdesc":"\n <p>The <code>deferred.promise()</code> method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request. The Promise exposes only the Deferred methods needed to attach additional handlers or determine the state (<code>then</code>, <code>done</code>, <code>fail</code>, <code>always</code>, <code>pipe</code>, <code>progress</code>, and <code>state</code>), but not ones that change the state (<code>resolve</code>, <code>reject</code>, <code>notify</code>, <code>resolveWith</code>, <code>rejectWith</code>, and <code>notifyWith</code>).</p>\n <p>If <code>target</code> is provided, <code>deferred.promise()</code> will attach the methods onto it and then return this object rather than create a new one. This can be useful to attach the Promise behavior to an object that already exists.</p>\n <p>If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point. Return <em>only</em> the Promise object via <code>deferred.promise()</code> so other code can register callbacks or inspect the current state.</p>\n <p>For more information, see the documentation for <a href=\"/category/deferred-object/\">Deferred object</a>.</p>\n "}]}