Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
devel:languages:perl:CPAN-P
perl-Promise-Me
perl-Promise-Me.spec
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File perl-Promise-Me.spec of Package perl-Promise-Me
# # spec file for package perl-Promise-Me # # Copyright (c) 2024 SUSE LLC # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed # upon. The license for this file, and modifications and additions to the # file, is the same license as for the pristine package itself (unless the # license for the pristine package is not an Open Source License, in which # case the license is the MIT License). An "Open Source License" is a # license that conforms to the Open Source Definition (Version 1.9) # published by the Open Source Initiative. # Please submit bugfixes or comments via https://bugs.opensuse.org/ # %define cpan_name Promise-Me Name: perl-Promise-Me Version: 0.4.11 Release: 0 License: Artistic-1.0 OR GPL-1.0-or-later Summary: Fork Based Promise with Asynchronous Execution, Async, Await and Shared Data URL: https://metacpan.org/release/%{cpan_name} Source0: https://cpan.metacpan.org/authors/id/J/JD/JDEGUEST/%{cpan_name}-v%{version}.tar.gz BuildArch: noarch BuildRequires: perl BuildRequires: perl-macros BuildRequires: perl(Clone) >= 0.45 BuildRequires: perl(Filter::Util::Call) >= 1.59 BuildRequires: perl(Module::Generic) >= v0.35.4 BuildRequires: perl(Module::Generic::File) >= v0.8.1 BuildRequires: perl(Nice::Try) >= v1.3.10 BuildRequires: perl(PPI) >= 1.278 BuildRequires: perl(Scalar::Util) >= 1.50 BuildRequires: perl(Storable::Improved) >= v0.1.3 BuildRequires: perl(Test2::IPC) >= 1.302190 BuildRequires: perl(Test2::V0) >= 0.000145 BuildRequires: perl(Test::More) >= 1.302162 BuildRequires: perl(Test::Pod) >= 1.52 BuildRequires: perl(Want) >= 0.29 BuildRequires: perl(curry) BuildRequires: perl(parent) Requires: perl(Clone) >= 0.45 Requires: perl(Filter::Util::Call) >= 1.59 Requires: perl(Module::Generic) >= v0.35.4 Requires: perl(Nice::Try) >= v1.3.10 Requires: perl(PPI) >= 1.278 Requires: perl(Scalar::Util) >= 1.50 Requires: perl(Storable::Improved) >= v0.1.3 Requires: perl(Want) >= 0.29 Requires: perl(curry) Requires: perl(parent) %{perl_requires} %description Promise::Me is an implementation of the JavaScript promise using fork for asynchronous tasks. Fork is great, because it is well supported by all operating systems (except AmigaOS, RISC OS and VMS) and effectively allows for asynchronous execution. While JavaScript has asynchronous execution at its core, which means that two consecutive lines of code will execute simultaneously, under perl, those two lines would be executed one after the other. For example: # Assuming the function getRemote makes an http query of a remote resource that takes time let response = getRemote('https://example.com/api'); console.log(response); Under JavaScript, this would yield: 'undefined', but in perl my $resp = $ua->get('https://example.com/api'); say( $resp ); Would correctly return the response object, but it will hang until it gets the returned object whereas in JavaScript, it would not wait. In JavaScript, because of this asynchronous execution, before people were using callback hooks, which resulted in "callback from hell", i.e. something like this[1]: getData(function(x){ getMoreData(x, function(y){ getMoreData(y, function(z){ ... }); }); }); [1] Taken from this at https://stackoverflow.com/questions/25098066/what-is-callback-hell-and-how- and-why-does-rx-solve-it And then, they came up with at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob jects/Promise, so that instead of wrapping your code in a callback function you get instead a promise object that gets called when certain events get triggered, like so[2]: const myPromise = new Promise((resolve, reject) => { setTimeout(() => { resolve('foo'); }, 300); }); myPromise .then(handleResolvedA, handleRejectedA) .then(handleResolvedB, handleRejectedB) .then(handleResolvedC, handleRejectedC); [2] Taken from at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob jects/Promise Chaining is easy to implement in perl and Promise::Me does it too. Where it gets more tricky is returning a promise immediately without waiting for further execution, i.e. a deferred promise, like the following in JavaScript: function getRemote(url) { let promise = new Promise((resolve, reject) => { setTimeout(() => reject(new Error("Whoops!")), 1000); }); // Maybe do some other stuff here return( promise ); } In this example, under JavaScript, the 'promise' will be returned immediately. However, under perl, the equivalent code would be executed sequentially. For example, using the excellent module Promise::ES6: sub get_remote { my $url = shift( @_ ); my $p = Promise::ES6->new(sub($res) { $res->( Promise::ES6->resolve(123) ); }); # Do some more work that would take some time return( $p ); } In the example above, the promise '$p' would not be returned until all the tasks are completed before the 'return' statement, contrary to JavaScript where it would be returned immediately. So, in perl people have started to use loop such as AnyEvent or IO::Async with "conditional variable" to get that asynchronous execution, but you need to use loops. For example (taken from Promise::AsyncAwait): use Promise::AsyncAwait; use Promise::XS; sub delay { my $secs = shift; my $d = Promise::XS::deferred(); my $timer; $timer = AnyEvent->timer( after => $secs, cb => sub { undef $timer; $d->resolve($secs); }, ); return $d->promise(); } async sub wait_plus_1 { my $num = await delay(0.01); return 1 + $num; } my $cv = AnyEvent->condvar(); wait_plus_1()->then($cv, sub { $cv->croak(@_) }); my ($got) = $cv->recv(); So, in the midst of this, I have tried to provide something without event loop by using fork instead as exemplified in the SYNOPSIS For a framework to do asynchronous tasks, you might also be interested in Coro, from at https://metacpan.org/author/MLEHMANN original author of AnyEvent event loop. %prep %autosetup -n %{cpan_name}-v%{version} %build perl Makefile.PL INSTALLDIRS=vendor %make_build %check make test %install %perl_make_install %perl_process_packlist %perl_gen_filelist %files -f %{name}.files %doc Changes CONTRIBUTING.md README README.md %license LICENSE %changelog
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor